|
| 1 | +/* |
| 2 | + * |
| 3 | + * This program is free software; you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation; either version 2 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * Description: |
| 14 | + * A pddf kernel driver module for FPGA connected to the CPU by I2C bus |
| 15 | + */ |
| 16 | + |
| 17 | +#include <linux/module.h> |
| 18 | +#include <linux/i2c.h> |
| 19 | +#include <linux/slab.h> |
| 20 | +#include <linux/list.h> |
| 21 | +#include <linux/dmi.h> |
| 22 | +#include "pddf_fpgai2c_defs.h" |
| 23 | + |
| 24 | +extern PDDF_FPGAI2C_DATA pddf_fpgai2c_data; |
| 25 | + |
| 26 | + |
| 27 | +static LIST_HEAD(fpgai2c_client_list); |
| 28 | +static struct mutex list_lock; |
| 29 | + |
| 30 | +struct fpgai2c_client_node { |
| 31 | + struct i2c_client *client; |
| 32 | + struct list_head list; |
| 33 | +}; |
| 34 | + |
| 35 | +int board_i2c_fpga_read(unsigned short fpga_addr, u8 reg) |
| 36 | +{ |
| 37 | + struct list_head *list_node = NULL; |
| 38 | + struct fpgai2c_client_node *fpga_node = NULL; |
| 39 | + int ret = -EPERM; |
| 40 | + |
| 41 | + |
| 42 | + mutex_lock(&list_lock); |
| 43 | + |
| 44 | + list_for_each(list_node, &fpgai2c_client_list) |
| 45 | + { |
| 46 | + fpga_node = list_entry(list_node, struct fpgai2c_client_node, list); |
| 47 | + |
| 48 | + if (fpga_node->client->addr == fpga_addr) { |
| 49 | + ret = i2c_smbus_read_byte_data(fpga_node->client, reg); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + mutex_unlock(&list_lock); |
| 55 | + |
| 56 | + return ret; |
| 57 | +} |
| 58 | +EXPORT_SYMBOL(board_i2c_fpga_read); |
| 59 | + |
| 60 | +int board_i2c_fpga_write(unsigned short fpga_addr, u8 reg, u8 value) |
| 61 | +{ |
| 62 | + struct list_head *list_node = NULL; |
| 63 | + struct fpgai2c_client_node *fpga_node = NULL; |
| 64 | + int ret = -EIO; |
| 65 | + |
| 66 | + |
| 67 | + mutex_lock(&list_lock); |
| 68 | + |
| 69 | + list_for_each(list_node, &fpgai2c_client_list) |
| 70 | + { |
| 71 | + fpga_node = list_entry(list_node, struct fpgai2c_client_node, list); |
| 72 | + |
| 73 | + if (fpga_node->client->addr == fpga_addr) { |
| 74 | + ret = i2c_smbus_write_byte_data(fpga_node->client, reg, value); |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + mutex_unlock(&list_lock); |
| 80 | + |
| 81 | + return ret; |
| 82 | +} |
| 83 | +EXPORT_SYMBOL(board_i2c_fpga_write); |
| 84 | + |
| 85 | +ssize_t regval_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 86 | +{ |
| 87 | + int len = 0; |
| 88 | + struct i2c_client *client = to_i2c_client(dev); |
| 89 | + |
| 90 | + mutex_lock(&pddf_fpgai2c_data.fpga_lock); |
| 91 | + // Put code here to read the register value and print it |
| 92 | + if (pddf_fpgai2c_data.reg_addr!=0) |
| 93 | + len = sprintf(buf, "0x%2.2x\n", board_i2c_fpga_read(client->addr, pddf_fpgai2c_data.reg_addr)); |
| 94 | + else |
| 95 | + len = sprintf(buf, "xx\n"); |
| 96 | + |
| 97 | + mutex_unlock(&pddf_fpgai2c_data.fpga_lock); |
| 98 | + return len; |
| 99 | +} |
| 100 | + |
| 101 | +static DEVICE_ATTR_RO(regval); |
| 102 | + |
| 103 | +static struct attribute *fpgai2c_attrs[] = { |
| 104 | + &dev_attr_regval.attr, |
| 105 | + NULL, |
| 106 | +}; |
| 107 | + |
| 108 | +static struct attribute_group fpgai2c_attribute_group = { |
| 109 | + .attrs = fpgai2c_attrs, |
| 110 | +}; |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +/* Addresses scanned for board_i2c_fpga |
| 116 | + */ |
| 117 | +static const unsigned short normal_i2c[] = { I2C_CLIENT_END }; |
| 118 | + |
| 119 | +static void board_i2c_fpga_add_client(struct i2c_client *client) |
| 120 | +{ |
| 121 | + struct fpgai2c_client_node *node = kzalloc(sizeof(struct fpgai2c_client_node), GFP_KERNEL); |
| 122 | + |
| 123 | + if (!node) { |
| 124 | + dev_dbg(&client->dev, "Can't allocate fpgai2c_client_node (0x%x)\n", client->addr); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + node->client = client; |
| 129 | + |
| 130 | + mutex_lock(&list_lock); |
| 131 | + list_add(&node->list, &fpgai2c_client_list); |
| 132 | + mutex_unlock(&list_lock); |
| 133 | +} |
| 134 | + |
| 135 | +static void board_i2c_fpga_remove_client(struct i2c_client *client) |
| 136 | +{ |
| 137 | + struct list_head *list_node = NULL; |
| 138 | + struct fpgai2c_client_node *fpga_node = NULL; |
| 139 | + int found = 0; |
| 140 | + |
| 141 | + mutex_lock(&list_lock); |
| 142 | + |
| 143 | + list_for_each(list_node, &fpgai2c_client_list) |
| 144 | + { |
| 145 | + fpga_node = list_entry(list_node, struct fpgai2c_client_node, list); |
| 146 | + |
| 147 | + if (fpga_node->client == client) { |
| 148 | + found = 1; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (found) { |
| 154 | + list_del(list_node); |
| 155 | + kfree(fpga_node); |
| 156 | + } |
| 157 | + |
| 158 | + mutex_unlock(&list_lock); |
| 159 | +} |
| 160 | + |
| 161 | +static int board_i2c_fpga_probe(struct i2c_client *client, |
| 162 | + const struct i2c_device_id *dev_id) |
| 163 | +{ |
| 164 | + int status; |
| 165 | + |
| 166 | + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 167 | + dev_dbg(&client->dev, "i2c_check_functionality failed (0x%x)\n", client->addr); |
| 168 | + status = -EIO; |
| 169 | + goto exit; |
| 170 | + } |
| 171 | + |
| 172 | + /* Register sysfs hooks */ |
| 173 | + status = sysfs_create_group(&client->dev.kobj, &fpgai2c_attribute_group); |
| 174 | + if (status) { |
| 175 | + goto exit; |
| 176 | + } |
| 177 | + |
| 178 | + dev_dbg(&client->dev, "chip found\n"); |
| 179 | + board_i2c_fpga_add_client(client); |
| 180 | + |
| 181 | + return 0; |
| 182 | + |
| 183 | +exit: |
| 184 | + return status; |
| 185 | +} |
| 186 | + |
| 187 | +static int board_i2c_fpga_remove(struct i2c_client *client) |
| 188 | +{ |
| 189 | + sysfs_remove_group(&client->dev.kobj, &fpgai2c_attribute_group); |
| 190 | + board_i2c_fpga_remove_client(client); |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
| 194 | + |
| 195 | +static const struct i2c_device_id board_i2c_fpga_id[] = { |
| 196 | + { "i2c_fpga", 0 }, |
| 197 | + {} |
| 198 | +}; |
| 199 | +MODULE_DEVICE_TABLE(i2c, board_i2c_fpga_id); |
| 200 | + |
| 201 | +static struct i2c_driver board_i2c_fpga_driver = { |
| 202 | + .class = I2C_CLASS_HWMON, |
| 203 | + .driver = { |
| 204 | + .name = "i2c_fpga", |
| 205 | + }, |
| 206 | + .probe = board_i2c_fpga_probe, |
| 207 | + .remove = board_i2c_fpga_remove, |
| 208 | + .id_table = board_i2c_fpga_id, |
| 209 | + .address_list = normal_i2c, |
| 210 | +}; |
| 211 | + |
| 212 | +static int __init board_i2c_fpga_init(void) |
| 213 | +{ |
| 214 | + mutex_init(&list_lock); |
| 215 | + return i2c_add_driver(&board_i2c_fpga_driver); |
| 216 | +} |
| 217 | + |
| 218 | +static void __exit board_i2c_fpga_exit(void) |
| 219 | +{ |
| 220 | + i2c_del_driver(&board_i2c_fpga_driver); |
| 221 | +} |
| 222 | + |
| 223 | +MODULE_AUTHOR("Broadcom"); |
| 224 | +MODULE_DESCRIPTION("board_i2c_fpga driver"); |
| 225 | +MODULE_LICENSE("GPL"); |
| 226 | + |
| 227 | +module_init(board_i2c_fpga_init); |
| 228 | +module_exit(board_i2c_fpga_exit); |
0 commit comments