2 * I2C slave mode EEPROM simulator
4 * Copyright (C) 2014 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
5 * Copyright (C) 2014 by Renesas Electronics Corporation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; version 2 of the License.
11 * Because most IP blocks can only detect one I2C slave address anyhow, this
12 * driver does not support simulating EEPROM types which take more than one
13 * address. It is prepared to simulate bigger EEPROMs with an internal 16 bit
14 * pointer, yet implementation is deferred until the need actually arises.
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/sysfs.h>
26 struct bin_attribute bin
;
28 spinlock_t buffer_lock
;
33 static int i2c_slave_eeprom_slave_cb(struct i2c_client
*client
,
34 enum i2c_slave_event event
, u8
*val
)
36 struct eeprom_data
*eeprom
= i2c_get_clientdata(client
);
39 case I2C_SLAVE_WRITE_RECEIVED
:
40 if (eeprom
->first_write
) {
41 eeprom
->buffer_idx
= *val
;
42 eeprom
->first_write
= false;
44 spin_lock(&eeprom
->buffer_lock
);
45 eeprom
->buffer
[eeprom
->buffer_idx
++] = *val
;
46 spin_unlock(&eeprom
->buffer_lock
);
50 case I2C_SLAVE_READ_PROCESSED
:
51 /* The previous byte made it to the bus, get next one */
54 case I2C_SLAVE_READ_REQUESTED
:
55 spin_lock(&eeprom
->buffer_lock
);
56 *val
= eeprom
->buffer
[eeprom
->buffer_idx
];
57 spin_unlock(&eeprom
->buffer_lock
);
59 * Do not increment buffer_idx here, because we don't know if
60 * this byte will be actually used. Read Linux I2C slave docs
66 case I2C_SLAVE_WRITE_REQUESTED
:
67 eeprom
->first_write
= true;
77 static ssize_t
i2c_slave_eeprom_bin_read(struct file
*filp
, struct kobject
*kobj
,
78 struct bin_attribute
*attr
, char *buf
, loff_t off
, size_t count
)
80 struct eeprom_data
*eeprom
;
83 eeprom
= dev_get_drvdata(container_of(kobj
, struct device
, kobj
));
85 spin_lock_irqsave(&eeprom
->buffer_lock
, flags
);
86 memcpy(buf
, &eeprom
->buffer
[off
], count
);
87 spin_unlock_irqrestore(&eeprom
->buffer_lock
, flags
);
92 static ssize_t
i2c_slave_eeprom_bin_write(struct file
*filp
, struct kobject
*kobj
,
93 struct bin_attribute
*attr
, char *buf
, loff_t off
, size_t count
)
95 struct eeprom_data
*eeprom
;
98 eeprom
= dev_get_drvdata(container_of(kobj
, struct device
, kobj
));
100 spin_lock_irqsave(&eeprom
->buffer_lock
, flags
);
101 memcpy(&eeprom
->buffer
[off
], buf
, count
);
102 spin_unlock_irqrestore(&eeprom
->buffer_lock
, flags
);
107 static int i2c_slave_eeprom_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
109 struct eeprom_data
*eeprom
;
111 unsigned size
= id
->driver_data
;
113 eeprom
= devm_kzalloc(&client
->dev
, sizeof(struct eeprom_data
) + size
, GFP_KERNEL
);
117 eeprom
->first_write
= true;
118 spin_lock_init(&eeprom
->buffer_lock
);
119 i2c_set_clientdata(client
, eeprom
);
121 sysfs_bin_attr_init(&eeprom
->bin
);
122 eeprom
->bin
.attr
.name
= "slave-eeprom";
123 eeprom
->bin
.attr
.mode
= S_IRUSR
| S_IWUSR
;
124 eeprom
->bin
.read
= i2c_slave_eeprom_bin_read
;
125 eeprom
->bin
.write
= i2c_slave_eeprom_bin_write
;
126 eeprom
->bin
.size
= size
;
128 ret
= sysfs_create_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
132 ret
= i2c_slave_register(client
, i2c_slave_eeprom_slave_cb
);
134 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
141 static int i2c_slave_eeprom_remove(struct i2c_client
*client
)
143 struct eeprom_data
*eeprom
= i2c_get_clientdata(client
);
145 i2c_slave_unregister(client
);
146 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom
->bin
);
151 static const struct i2c_device_id i2c_slave_eeprom_id
[] = {
152 { "slave-24c02", 2048 / 8 },
155 MODULE_DEVICE_TABLE(i2c
, i2c_slave_eeprom_id
);
157 static struct i2c_driver i2c_slave_eeprom_driver
= {
159 .name
= "i2c-slave-eeprom",
160 .owner
= THIS_MODULE
,
162 .probe
= i2c_slave_eeprom_probe
,
163 .remove
= i2c_slave_eeprom_remove
,
164 .id_table
= i2c_slave_eeprom_id
,
166 module_i2c_driver(i2c_slave_eeprom_driver
);
168 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
169 MODULE_DESCRIPTION("I2C slave mode EEPROM simulator");
170 MODULE_LICENSE("GPL v2");