Lines Matching +full:eeprom +full:- +full:name

1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C slave mode EEPROM simulator
5 * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
9 * this driver does not support simulating EEPROM types which take more than
15 * The ST-M24C64 sends only 0xff then. Needs verification with other
43 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | ((_len) - 1))
48 struct eeprom_data *eeprom = i2c_get_clientdata(client); in i2c_slave_eeprom_slave_cb() local
52 if (eeprom->idx_write_cnt < eeprom->num_address_bytes) { in i2c_slave_eeprom_slave_cb()
53 if (eeprom->idx_write_cnt == 0) in i2c_slave_eeprom_slave_cb()
54 eeprom->buffer_idx = 0; in i2c_slave_eeprom_slave_cb()
55 eeprom->buffer_idx = *val | (eeprom->buffer_idx << 8); in i2c_slave_eeprom_slave_cb()
56 eeprom->idx_write_cnt++; in i2c_slave_eeprom_slave_cb()
58 if (!eeprom->read_only) { in i2c_slave_eeprom_slave_cb()
59 spin_lock(&eeprom->buffer_lock); in i2c_slave_eeprom_slave_cb()
60 eeprom->buffer[eeprom->buffer_idx++ & eeprom->address_mask] = *val; in i2c_slave_eeprom_slave_cb()
61 spin_unlock(&eeprom->buffer_lock); in i2c_slave_eeprom_slave_cb()
68 eeprom->buffer_idx++; in i2c_slave_eeprom_slave_cb()
71 spin_lock(&eeprom->buffer_lock); in i2c_slave_eeprom_slave_cb()
72 *val = eeprom->buffer[eeprom->buffer_idx & eeprom->address_mask]; in i2c_slave_eeprom_slave_cb()
73 spin_unlock(&eeprom->buffer_lock); in i2c_slave_eeprom_slave_cb()
83 eeprom->idx_write_cnt = 0; in i2c_slave_eeprom_slave_cb()
96 struct eeprom_data *eeprom; in i2c_slave_eeprom_bin_read() local
99 eeprom = dev_get_drvdata(kobj_to_dev(kobj)); in i2c_slave_eeprom_bin_read()
101 spin_lock_irqsave(&eeprom->buffer_lock, flags); in i2c_slave_eeprom_bin_read()
102 memcpy(buf, &eeprom->buffer[off], count); in i2c_slave_eeprom_bin_read()
103 spin_unlock_irqrestore(&eeprom->buffer_lock, flags); in i2c_slave_eeprom_bin_read()
111 struct eeprom_data *eeprom; in i2c_slave_eeprom_bin_write() local
114 eeprom = dev_get_drvdata(kobj_to_dev(kobj)); in i2c_slave_eeprom_bin_write()
116 spin_lock_irqsave(&eeprom->buffer_lock, flags); in i2c_slave_eeprom_bin_write()
117 memcpy(&eeprom->buffer[off], buf, count); in i2c_slave_eeprom_bin_write()
118 spin_unlock_irqrestore(&eeprom->buffer_lock, flags); in i2c_slave_eeprom_bin_write()
123 static int i2c_slave_init_eeprom_data(struct eeprom_data *eeprom, struct i2c_client *client, in i2c_slave_init_eeprom_data() argument
128 int ret = device_property_read_string(&client->dev, "firmware-name", &eeprom_data); in i2c_slave_init_eeprom_data()
131 ret = request_firmware_into_buf(&fw, eeprom_data, &client->dev, in i2c_slave_init_eeprom_data()
132 eeprom->buffer, size); in i2c_slave_init_eeprom_data()
137 /* An empty eeprom typically has all bits set to 1 */ in i2c_slave_init_eeprom_data()
138 memset(eeprom->buffer, 0xff, size); in i2c_slave_init_eeprom_data()
146 struct eeprom_data *eeprom; in i2c_slave_eeprom_probe() local
148 unsigned int size = FIELD_GET(I2C_SLAVE_BYTELEN, id->driver_data) + 1; in i2c_slave_eeprom_probe()
149 unsigned int flag_addr16 = FIELD_GET(I2C_SLAVE_FLAG_ADDR16, id->driver_data); in i2c_slave_eeprom_probe()
151 eeprom = devm_kzalloc(&client->dev, sizeof(struct eeprom_data) + size, GFP_KERNEL); in i2c_slave_eeprom_probe()
152 if (!eeprom) in i2c_slave_eeprom_probe()
153 return -ENOMEM; in i2c_slave_eeprom_probe()
155 eeprom->num_address_bytes = flag_addr16 ? 2 : 1; in i2c_slave_eeprom_probe()
156 eeprom->address_mask = size - 1; in i2c_slave_eeprom_probe()
157 eeprom->read_only = FIELD_GET(I2C_SLAVE_FLAG_RO, id->driver_data); in i2c_slave_eeprom_probe()
158 spin_lock_init(&eeprom->buffer_lock); in i2c_slave_eeprom_probe()
159 i2c_set_clientdata(client, eeprom); in i2c_slave_eeprom_probe()
161 ret = i2c_slave_init_eeprom_data(eeprom, client, size); in i2c_slave_eeprom_probe()
165 sysfs_bin_attr_init(&eeprom->bin); in i2c_slave_eeprom_probe()
166 eeprom->bin.attr.name = "slave-eeprom"; in i2c_slave_eeprom_probe()
167 eeprom->bin.attr.mode = S_IRUSR | S_IWUSR; in i2c_slave_eeprom_probe()
168 eeprom->bin.read = i2c_slave_eeprom_bin_read; in i2c_slave_eeprom_probe()
169 eeprom->bin.write = i2c_slave_eeprom_bin_write; in i2c_slave_eeprom_probe()
170 eeprom->bin.size = size; in i2c_slave_eeprom_probe()
172 ret = sysfs_create_bin_file(&client->dev.kobj, &eeprom->bin); in i2c_slave_eeprom_probe()
178 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin); in i2c_slave_eeprom_probe()
187 struct eeprom_data *eeprom = i2c_get_clientdata(client); in i2c_slave_eeprom_remove() local
190 sysfs_remove_bin_file(&client->dev.kobj, &eeprom->bin); in i2c_slave_eeprom_remove()
194 { "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, 0) },
195 { "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8, I2C_SLAVE_FLAG_RO) },
196 { "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
197 { "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
198 { "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
199 { "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
200 { "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
201 …{ "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) …
208 .name = "i2c-slave-eeprom",
216 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
217 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");