Skip to content

Commit 7274ec8

Browse files
metaliveblogtorvalds
authored andcommitted
memory_accessor: implement the new memory_accessor interface for I2C EEPROM
In the case of at24, the platform code registers a 'setup' callback with the at24_platform_data. When the at24 driver detects an EEPROM, it fills out the read and write functions of the memory_accessor and calls the setup callback passing the memory_accessor struct. The platform code can then use the read/write functions in the memory_accessor struct for reading and writing the EEPROM. Signed-off-by: Kevin Hilman <khilman@deeprootsystems.com> Cc: David Brownell <dbrownell@users.sourceforge.net> Cc: Jean Delvare <khali@linux-fr.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 06c421e commit 7274ec8

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎drivers/misc/eeprom/at24.c‎

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
struct at24_data {
5555
struct at24_platform_data chip;
56+
struct memory_accessor macc;
5657
bool use_smbus;
5758

5859
/*
@@ -225,14 +226,11 @@ static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
225226
return status;
226227
}
227228

228-
static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
229+
static ssize_t at24_read(struct at24_data *at24,
229230
char *buf, loff_t off, size_t count)
230231
{
231-
struct at24_data *at24;
232232
ssize_t retval = 0;
233233

234-
at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
235-
236234
if (unlikely(!count))
237235
return count;
238236

@@ -262,12 +260,14 @@ static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
262260
return retval;
263261
}
264262

263+
static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
264+
char *buf, loff_t off, size_t count)
265+
{
266+
struct at24_data *at24;
265267

266-
/*
267-
* REVISIT: export at24_bin{read,write}() to let other kernel code use
268-
* eeprom data. For example, it might hold a board's Ethernet address, or
269-
* board-specific calibration data generated on the manufacturing floor.
270-
*/
268+
at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
269+
return at24_read(at24, buf, off, count);
270+
}
271271

272272

273273
/*
@@ -347,14 +347,11 @@ static ssize_t at24_eeprom_write(struct at24_data *at24, char *buf,
347347
return -ETIMEDOUT;
348348
}
349349

350-
static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
350+
static ssize_t at24_write(struct at24_data *at24,
351351
char *buf, loff_t off, size_t count)
352352
{
353-
struct at24_data *at24;
354353
ssize_t retval = 0;
355354

356-
at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
357-
358355
if (unlikely(!count))
359356
return count;
360357

@@ -384,6 +381,39 @@ static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
384381
return retval;
385382
}
386383

384+
static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
385+
char *buf, loff_t off, size_t count)
386+
{
387+
struct at24_data *at24;
388+
389+
at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
390+
return at24_write(at24, buf, off, count);
391+
}
392+
393+
/*-------------------------------------------------------------------------*/
394+
395+
/*
396+
* This lets other kernel code access the eeprom data. For example, it
397+
* might hold a board's Ethernet address, or board-specific calibration
398+
* data generated on the manufacturing floor.
399+
*/
400+
401+
static ssize_t at24_macc_read(struct memory_accessor *macc, char *buf,
402+
off_t offset, size_t count)
403+
{
404+
struct at24_data *at24 = container_of(macc, struct at24_data, macc);
405+
406+
return at24_read(at24, buf, offset, count);
407+
}
408+
409+
static ssize_t at24_macc_write(struct memory_accessor *macc, char *buf,
410+
off_t offset, size_t count)
411+
{
412+
struct at24_data *at24 = container_of(macc, struct at24_data, macc);
413+
414+
return at24_write(at24, buf, offset, count);
415+
}
416+
387417
/*-------------------------------------------------------------------------*/
388418

389419
static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
@@ -413,6 +443,9 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
413443
* is recommended anyhow.
414444
*/
415445
chip.page_size = 1;
446+
447+
chip.setup = NULL;
448+
chip.context = NULL;
416449
}
417450

418451
if (!is_power_of_2(chip.byte_len))
@@ -463,13 +496,17 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
463496
at24->bin.read = at24_bin_read;
464497
at24->bin.size = chip.byte_len;
465498

499+
at24->macc.read = at24_macc_read;
500+
466501
writable = !(chip.flags & AT24_FLAG_READONLY);
467502
if (writable) {
468503
if (!use_smbus || i2c_check_functionality(client->adapter,
469504
I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
470505

471506
unsigned write_max = chip.page_size;
472507

508+
at24->macc.write = at24_macc_write;
509+
473510
at24->bin.write = at24_bin_write;
474511
at24->bin.attr.mode |= S_IWUSR;
475512

@@ -520,6 +557,10 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
520557
at24->write_max,
521558
use_smbus ? ", use_smbus" : "");
522559

560+
/* export data to kernel code */
561+
if (chip.setup)
562+
chip.setup(&at24->macc, chip.context);
563+
523564
return 0;
524565

525566
err_clients:

‎include/linux/i2c/at24.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _LINUX_AT24_H
33

44
#include <linux/types.h>
5+
#include <linux/memory.h>
56

67
/*
78
* As seen through Linux I2C, differences between the most common types of I2C
@@ -23,6 +24,9 @@ struct at24_platform_data {
2324
#define AT24_FLAG_READONLY 0x40 /* sysfs-entry will be read-only */
2425
#define AT24_FLAG_IRUGO 0x20 /* sysfs-entry will be world-readable */
2526
#define AT24_FLAG_TAKE8ADDR 0x10 /* take always 8 addresses (24c00) */
27+
28+
void (*setup)(struct memory_accessor *, void *context);
29+
void *context;
2630
};
2731

2832
#endif /* _LINUX_AT24_H */

0 commit comments

Comments
 (0)