Blackfin: fix SMP build error in start_thread()
[sfrench/cifs-2.6.git] / drivers / misc / eeprom / at24.c
1 /*
2  * at24.c - handle most I2C EEPROMs
3  *
4  * Copyright (C) 2005-2007 David Brownell
5  * Copyright (C) 2008 Wolfram Sang, Pengutronix
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/mutex.h>
18 #include <linux/sysfs.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/log2.h>
21 #include <linux/bitops.h>
22 #include <linux/jiffies.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c/at24.h>
25
26 /*
27  * I2C EEPROMs from most vendors are inexpensive and mostly interchangeable.
28  * Differences between different vendor product lines (like Atmel AT24C or
29  * MicroChip 24LC, etc) won't much matter for typical read/write access.
30  * There are also I2C RAM chips, likewise interchangeable. One example
31  * would be the PCF8570, which acts like a 24c02 EEPROM (256 bytes).
32  *
33  * However, misconfiguration can lose data. "Set 16-bit memory address"
34  * to a part with 8-bit addressing will overwrite data. Writing with too
35  * big a page size also loses data. And it's not safe to assume that the
36  * conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
37  * uses 0x51, for just one example.
38  *
39  * Accordingly, explicit board-specific configuration data should be used
40  * in almost all cases. (One partial exception is an SMBus used to access
41  * "SPD" data for DRAM sticks. Those only use 24c02 EEPROMs.)
42  *
43  * So this driver uses "new style" I2C driver binding, expecting to be
44  * told what devices exist. That may be in arch/X/mach-Y/board-Z.c or
45  * similar kernel-resident tables; or, configuration data coming from
46  * a bootloader.
47  *
48  * Other than binding model, current differences from "eeprom" driver are
49  * that this one handles write access and isn't restricted to 24c02 devices.
50  * It also handles larger devices (32 kbit and up) with two-byte addresses,
51  * which won't work on pure SMBus systems.
52  */
53
54 struct at24_data {
55         struct at24_platform_data chip;
56         struct memory_accessor macc;
57         bool use_smbus;
58
59         /*
60          * Lock protects against activities from other Linux tasks,
61          * but not from changes by other I2C masters.
62          */
63         struct mutex lock;
64         struct bin_attribute bin;
65
66         u8 *writebuf;
67         unsigned write_max;
68         unsigned num_addresses;
69
70         /*
71          * Some chips tie up multiple I2C addresses; dummy devices reserve
72          * them for us, and we'll use them with SMBus calls.
73          */
74         struct i2c_client *client[];
75 };
76
77 /*
78  * This parameter is to help this driver avoid blocking other drivers out
79  * of I2C for potentially troublesome amounts of time. With a 100 kHz I2C
80  * clock, one 256 byte read takes about 1/43 second which is excessive;
81  * but the 1/170 second it takes at 400 kHz may be quite reasonable; and
82  * at 1 MHz (Fm+) a 1/430 second delay could easily be invisible.
83  *
84  * This value is forced to be a power of two so that writes align on pages.
85  */
86 static unsigned io_limit = 128;
87 module_param(io_limit, uint, 0);
88 MODULE_PARM_DESC(io_limit, "Maximum bytes per I/O (default 128)");
89
90 /*
91  * Specs often allow 5 msec for a page write, sometimes 20 msec;
92  * it's important to recover from write timeouts.
93  */
94 static unsigned write_timeout = 25;
95 module_param(write_timeout, uint, 0);
96 MODULE_PARM_DESC(write_timeout, "Time (in ms) to try writes (default 25)");
97
98 #define AT24_SIZE_BYTELEN 5
99 #define AT24_SIZE_FLAGS 8
100
101 #define AT24_BITMASK(x) (BIT(x) - 1)
102
103 /* create non-zero magic value for given eeprom parameters */
104 #define AT24_DEVICE_MAGIC(_len, _flags)                 \
105         ((1 << AT24_SIZE_FLAGS | (_flags))              \
106             << AT24_SIZE_BYTELEN | ilog2(_len))
107
108 static const struct i2c_device_id at24_ids[] = {
109         /* needs 8 addresses as A0-A2 are ignored */
110         { "24c00", AT24_DEVICE_MAGIC(128 / 8, AT24_FLAG_TAKE8ADDR) },
111         /* old variants can't be handled with this generic entry! */
112         { "24c01", AT24_DEVICE_MAGIC(1024 / 8, 0) },
113         { "24c02", AT24_DEVICE_MAGIC(2048 / 8, 0) },
114         /* spd is a 24c02 in memory DIMMs */
115         { "spd", AT24_DEVICE_MAGIC(2048 / 8,
116                 AT24_FLAG_READONLY | AT24_FLAG_IRUGO) },
117         { "24c04", AT24_DEVICE_MAGIC(4096 / 8, 0) },
118         /* 24rf08 quirk is handled at i2c-core */
119         { "24c08", AT24_DEVICE_MAGIC(8192 / 8, 0) },
120         { "24c16", AT24_DEVICE_MAGIC(16384 / 8, 0) },
121         { "24c32", AT24_DEVICE_MAGIC(32768 / 8, AT24_FLAG_ADDR16) },
122         { "24c64", AT24_DEVICE_MAGIC(65536 / 8, AT24_FLAG_ADDR16) },
123         { "24c128", AT24_DEVICE_MAGIC(131072 / 8, AT24_FLAG_ADDR16) },
124         { "24c256", AT24_DEVICE_MAGIC(262144 / 8, AT24_FLAG_ADDR16) },
125         { "24c512", AT24_DEVICE_MAGIC(524288 / 8, AT24_FLAG_ADDR16) },
126         { "24c1024", AT24_DEVICE_MAGIC(1048576 / 8, AT24_FLAG_ADDR16) },
127         { "at24", 0 },
128         { /* END OF LIST */ }
129 };
130 MODULE_DEVICE_TABLE(i2c, at24_ids);
131
132 /*-------------------------------------------------------------------------*/
133
134 /*
135  * This routine supports chips which consume multiple I2C addresses. It
136  * computes the addressing information to be used for a given r/w request.
137  * Assumes that sanity checks for offset happened at sysfs-layer.
138  */
139 static struct i2c_client *at24_translate_offset(struct at24_data *at24,
140                 unsigned *offset)
141 {
142         unsigned i;
143
144         if (at24->chip.flags & AT24_FLAG_ADDR16) {
145                 i = *offset >> 16;
146                 *offset &= 0xffff;
147         } else {
148                 i = *offset >> 8;
149                 *offset &= 0xff;
150         }
151
152         return at24->client[i];
153 }
154
155 static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf,
156                 unsigned offset, size_t count)
157 {
158         struct i2c_msg msg[2];
159         u8 msgbuf[2];
160         struct i2c_client *client;
161         int status, i;
162
163         memset(msg, 0, sizeof(msg));
164
165         /*
166          * REVISIT some multi-address chips don't rollover page reads to
167          * the next slave address, so we may need to truncate the count.
168          * Those chips might need another quirk flag.
169          *
170          * If the real hardware used four adjacent 24c02 chips and that
171          * were misconfigured as one 24c08, that would be a similar effect:
172          * one "eeprom" file not four, but larger reads would fail when
173          * they crossed certain pages.
174          */
175
176         /*
177          * Slave address and byte offset derive from the offset. Always
178          * set the byte address; on a multi-master board, another master
179          * may have changed the chip's "current" address pointer.
180          */
181         client = at24_translate_offset(at24, &offset);
182
183         if (count > io_limit)
184                 count = io_limit;
185
186         /* Smaller eeproms can work given some SMBus extension calls */
187         if (at24->use_smbus) {
188                 if (count > I2C_SMBUS_BLOCK_MAX)
189                         count = I2C_SMBUS_BLOCK_MAX;
190                 status = i2c_smbus_read_i2c_block_data(client, offset,
191                                 count, buf);
192                 dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n",
193                                 count, offset, status);
194                 return (status < 0) ? -EIO : status;
195         }
196
197         /*
198          * When we have a better choice than SMBus calls, use a combined
199          * I2C message. Write address; then read up to io_limit data bytes.
200          * Note that read page rollover helps us here (unlike writes).
201          * msgbuf is u8 and will cast to our needs.
202          */
203         i = 0;
204         if (at24->chip.flags & AT24_FLAG_ADDR16)
205                 msgbuf[i++] = offset >> 8;
206         msgbuf[i++] = offset;
207
208         msg[0].addr = client->addr;
209         msg[0].buf = msgbuf;
210         msg[0].len = i;
211
212         msg[1].addr = client->addr;
213         msg[1].flags = I2C_M_RD;
214         msg[1].buf = buf;
215         msg[1].len = count;
216
217         status = i2c_transfer(client->adapter, msg, 2);
218         dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n",
219                         count, offset, status);
220
221         if (status == 2)
222                 return count;
223         else if (status >= 0)
224                 return -EIO;
225         else
226                 return status;
227 }
228
229 static ssize_t at24_read(struct at24_data *at24,
230                 char *buf, loff_t off, size_t count)
231 {
232         ssize_t retval = 0;
233
234         if (unlikely(!count))
235                 return count;
236
237         /*
238          * Read data from chip, protecting against concurrent updates
239          * from this host, but not from other I2C masters.
240          */
241         mutex_lock(&at24->lock);
242
243         while (count) {
244                 ssize_t status;
245
246                 status = at24_eeprom_read(at24, buf, off, count);
247                 if (status <= 0) {
248                         if (retval == 0)
249                                 retval = status;
250                         break;
251                 }
252                 buf += status;
253                 off += status;
254                 count -= status;
255                 retval += status;
256         }
257
258         mutex_unlock(&at24->lock);
259
260         return retval;
261 }
262
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;
267
268         at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
269         return at24_read(at24, buf, off, count);
270 }
271
272
273 /*
274  * Note that if the hardware write-protect pin is pulled high, the whole
275  * chip is normally write protected. But there are plenty of product
276  * variants here, including OTP fuses and partial chip protect.
277  *
278  * We only use page mode writes; the alternative is sloooow. This routine
279  * writes at most one page.
280  */
281 static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
282                 unsigned offset, size_t count)
283 {
284         struct i2c_client *client;
285         struct i2c_msg msg;
286         ssize_t status;
287         unsigned long timeout, write_time;
288         unsigned next_page;
289
290         /* Get corresponding I2C address and adjust offset */
291         client = at24_translate_offset(at24, &offset);
292
293         /* write_max is at most a page */
294         if (count > at24->write_max)
295                 count = at24->write_max;
296
297         /* Never roll over backwards, to the start of this page */
298         next_page = roundup(offset + 1, at24->chip.page_size);
299         if (offset + count > next_page)
300                 count = next_page - offset;
301
302         /* If we'll use I2C calls for I/O, set up the message */
303         if (!at24->use_smbus) {
304                 int i = 0;
305
306                 msg.addr = client->addr;
307                 msg.flags = 0;
308
309                 /* msg.buf is u8 and casts will mask the values */
310                 msg.buf = at24->writebuf;
311                 if (at24->chip.flags & AT24_FLAG_ADDR16)
312                         msg.buf[i++] = offset >> 8;
313
314                 msg.buf[i++] = offset;
315                 memcpy(&msg.buf[i], buf, count);
316                 msg.len = i + count;
317         }
318
319         /*
320          * Writes fail if the previous one didn't complete yet. We may
321          * loop a few times until this one succeeds, waiting at least
322          * long enough for one entire page write to work.
323          */
324         timeout = jiffies + msecs_to_jiffies(write_timeout);
325         do {
326                 write_time = jiffies;
327                 if (at24->use_smbus) {
328                         status = i2c_smbus_write_i2c_block_data(client,
329                                         offset, count, buf);
330                         if (status == 0)
331                                 status = count;
332                 } else {
333                         status = i2c_transfer(client->adapter, &msg, 1);
334                         if (status == 1)
335                                 status = count;
336                 }
337                 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
338                                 count, offset, status, jiffies);
339
340                 if (status == count)
341                         return count;
342
343                 /* REVISIT: at HZ=100, this is sloooow */
344                 msleep(1);
345         } while (time_before(write_time, timeout));
346
347         return -ETIMEDOUT;
348 }
349
350 static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
351                           size_t count)
352 {
353         ssize_t retval = 0;
354
355         if (unlikely(!count))
356                 return count;
357
358         /*
359          * Write data to chip, protecting against concurrent updates
360          * from this host, but not from other I2C masters.
361          */
362         mutex_lock(&at24->lock);
363
364         while (count) {
365                 ssize_t status;
366
367                 status = at24_eeprom_write(at24, buf, off, count);
368                 if (status <= 0) {
369                         if (retval == 0)
370                                 retval = status;
371                         break;
372                 }
373                 buf += status;
374                 off += status;
375                 count -= status;
376                 retval += status;
377         }
378
379         mutex_unlock(&at24->lock);
380
381         return retval;
382 }
383
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, const 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
417 /*-------------------------------------------------------------------------*/
418
419 static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
420 {
421         struct at24_platform_data chip;
422         bool writable;
423         bool use_smbus = false;
424         struct at24_data *at24;
425         int err;
426         unsigned i, num_addresses;
427         kernel_ulong_t magic;
428
429         if (client->dev.platform_data) {
430                 chip = *(struct at24_platform_data *)client->dev.platform_data;
431         } else {
432                 if (!id->driver_data) {
433                         err = -ENODEV;
434                         goto err_out;
435                 }
436                 magic = id->driver_data;
437                 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
438                 magic >>= AT24_SIZE_BYTELEN;
439                 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
440                 /*
441                  * This is slow, but we can't know all eeproms, so we better
442                  * play safe. Specifying custom eeprom-types via platform_data
443                  * is recommended anyhow.
444                  */
445                 chip.page_size = 1;
446
447                 chip.setup = NULL;
448                 chip.context = NULL;
449         }
450
451         if (!is_power_of_2(chip.byte_len))
452                 dev_warn(&client->dev,
453                         "byte_len looks suspicious (no power of 2)!\n");
454         if (!is_power_of_2(chip.page_size))
455                 dev_warn(&client->dev,
456                         "page_size looks suspicious (no power of 2)!\n");
457
458         /* Use I2C operations unless we're stuck with SMBus extensions. */
459         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
460                 if (chip.flags & AT24_FLAG_ADDR16) {
461                         err = -EPFNOSUPPORT;
462                         goto err_out;
463                 }
464                 if (!i2c_check_functionality(client->adapter,
465                                 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
466                         err = -EPFNOSUPPORT;
467                         goto err_out;
468                 }
469                 use_smbus = true;
470         }
471
472         if (chip.flags & AT24_FLAG_TAKE8ADDR)
473                 num_addresses = 8;
474         else
475                 num_addresses = DIV_ROUND_UP(chip.byte_len,
476                         (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
477
478         at24 = kzalloc(sizeof(struct at24_data) +
479                 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
480         if (!at24) {
481                 err = -ENOMEM;
482                 goto err_out;
483         }
484
485         mutex_init(&at24->lock);
486         at24->use_smbus = use_smbus;
487         at24->chip = chip;
488         at24->num_addresses = num_addresses;
489
490         /*
491          * Export the EEPROM bytes through sysfs, since that's convenient.
492          * By default, only root should see the data (maybe passwords etc)
493          */
494         at24->bin.attr.name = "eeprom";
495         at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
496         at24->bin.read = at24_bin_read;
497         at24->bin.size = chip.byte_len;
498
499         at24->macc.read = at24_macc_read;
500
501         writable = !(chip.flags & AT24_FLAG_READONLY);
502         if (writable) {
503                 if (!use_smbus || i2c_check_functionality(client->adapter,
504                                 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
505
506                         unsigned write_max = chip.page_size;
507
508                         at24->macc.write = at24_macc_write;
509
510                         at24->bin.write = at24_bin_write;
511                         at24->bin.attr.mode |= S_IWUSR;
512
513                         if (write_max > io_limit)
514                                 write_max = io_limit;
515                         if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
516                                 write_max = I2C_SMBUS_BLOCK_MAX;
517                         at24->write_max = write_max;
518
519                         /* buffer (data + address at the beginning) */
520                         at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
521                         if (!at24->writebuf) {
522                                 err = -ENOMEM;
523                                 goto err_struct;
524                         }
525                 } else {
526                         dev_warn(&client->dev,
527                                 "cannot write due to controller restrictions.");
528                 }
529         }
530
531         at24->client[0] = client;
532
533         /* use dummy devices for multiple-address chips */
534         for (i = 1; i < num_addresses; i++) {
535                 at24->client[i] = i2c_new_dummy(client->adapter,
536                                         client->addr + i);
537                 if (!at24->client[i]) {
538                         dev_err(&client->dev, "address 0x%02x unavailable\n",
539                                         client->addr + i);
540                         err = -EADDRINUSE;
541                         goto err_clients;
542                 }
543         }
544
545         err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
546         if (err)
547                 goto err_clients;
548
549         i2c_set_clientdata(client, at24);
550
551         dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
552                 at24->bin.size, client->name,
553                 writable ? "(writable)" : "(read-only)");
554         dev_dbg(&client->dev,
555                 "page_size %d, num_addresses %d, write_max %d%s\n",
556                 chip.page_size, num_addresses,
557                 at24->write_max,
558                 use_smbus ? ", use_smbus" : "");
559
560         /* export data to kernel code */
561         if (chip.setup)
562                 chip.setup(&at24->macc, chip.context);
563
564         return 0;
565
566 err_clients:
567         for (i = 1; i < num_addresses; i++)
568                 if (at24->client[i])
569                         i2c_unregister_device(at24->client[i]);
570
571         kfree(at24->writebuf);
572 err_struct:
573         kfree(at24);
574 err_out:
575         dev_dbg(&client->dev, "probe error %d\n", err);
576         return err;
577 }
578
579 static int __devexit at24_remove(struct i2c_client *client)
580 {
581         struct at24_data *at24;
582         int i;
583
584         at24 = i2c_get_clientdata(client);
585         sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
586
587         for (i = 1; i < at24->num_addresses; i++)
588                 i2c_unregister_device(at24->client[i]);
589
590         kfree(at24->writebuf);
591         kfree(at24);
592         i2c_set_clientdata(client, NULL);
593         return 0;
594 }
595
596 /*-------------------------------------------------------------------------*/
597
598 static struct i2c_driver at24_driver = {
599         .driver = {
600                 .name = "at24",
601                 .owner = THIS_MODULE,
602         },
603         .probe = at24_probe,
604         .remove = __devexit_p(at24_remove),
605         .id_table = at24_ids,
606 };
607
608 static int __init at24_init(void)
609 {
610         io_limit = rounddown_pow_of_two(io_limit);
611         return i2c_add_driver(&at24_driver);
612 }
613 module_init(at24_init);
614
615 static void __exit at24_exit(void)
616 {
617         i2c_del_driver(&at24_driver);
618 }
619 module_exit(at24_exit);
620
621 MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
622 MODULE_AUTHOR("David Brownell and Wolfram Sang");
623 MODULE_LICENSE("GPL");