i2c-nforce2: Remove redundant error messages on ACPI conflict
[pandora-kernel.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         int 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         unsigned long timeout, read_time;
162         int status, i;
163
164         memset(msg, 0, sizeof(msg));
165
166         /*
167          * REVISIT some multi-address chips don't rollover page reads to
168          * the next slave address, so we may need to truncate the count.
169          * Those chips might need another quirk flag.
170          *
171          * If the real hardware used four adjacent 24c02 chips and that
172          * were misconfigured as one 24c08, that would be a similar effect:
173          * one "eeprom" file not four, but larger reads would fail when
174          * they crossed certain pages.
175          */
176
177         /*
178          * Slave address and byte offset derive from the offset. Always
179          * set the byte address; on a multi-master board, another master
180          * may have changed the chip's "current" address pointer.
181          */
182         client = at24_translate_offset(at24, &offset);
183
184         if (count > io_limit)
185                 count = io_limit;
186
187         switch (at24->use_smbus) {
188         case I2C_SMBUS_I2C_BLOCK_DATA:
189                 /* Smaller eeproms can work given some SMBus extension calls */
190                 if (count > I2C_SMBUS_BLOCK_MAX)
191                         count = I2C_SMBUS_BLOCK_MAX;
192                 break;
193         case I2C_SMBUS_WORD_DATA:
194                 count = 2;
195                 break;
196         case I2C_SMBUS_BYTE_DATA:
197                 count = 1;
198                 break;
199         default:
200                 /*
201                  * When we have a better choice than SMBus calls, use a
202                  * combined I2C message. Write address; then read up to
203                  * io_limit data bytes. Note that read page rollover helps us
204                  * here (unlike writes). msgbuf is u8 and will cast to our
205                  * needs.
206                  */
207                 i = 0;
208                 if (at24->chip.flags & AT24_FLAG_ADDR16)
209                         msgbuf[i++] = offset >> 8;
210                 msgbuf[i++] = offset;
211
212                 msg[0].addr = client->addr;
213                 msg[0].buf = msgbuf;
214                 msg[0].len = i;
215
216                 msg[1].addr = client->addr;
217                 msg[1].flags = I2C_M_RD;
218                 msg[1].buf = buf;
219                 msg[1].len = count;
220         }
221
222         /*
223          * Reads fail if the previous write didn't complete yet. We may
224          * loop a few times until this one succeeds, waiting at least
225          * long enough for one entire page write to work.
226          */
227         timeout = jiffies + msecs_to_jiffies(write_timeout);
228         do {
229                 read_time = jiffies;
230                 switch (at24->use_smbus) {
231                 case I2C_SMBUS_I2C_BLOCK_DATA:
232                         status = i2c_smbus_read_i2c_block_data(client, offset,
233                                         count, buf);
234                         break;
235                 case I2C_SMBUS_WORD_DATA:
236                         status = i2c_smbus_read_word_data(client, offset);
237                         if (status >= 0) {
238                                 buf[0] = status & 0xff;
239                                 buf[1] = status >> 8;
240                                 status = count;
241                         }
242                         break;
243                 case I2C_SMBUS_BYTE_DATA:
244                         status = i2c_smbus_read_byte_data(client, offset);
245                         if (status >= 0) {
246                                 buf[0] = status;
247                                 status = count;
248                         }
249                         break;
250                 default:
251                         status = i2c_transfer(client->adapter, msg, 2);
252                         if (status == 2)
253                                 status = count;
254                 }
255                 dev_dbg(&client->dev, "read %zu@%d --> %d (%ld)\n",
256                                 count, offset, status, jiffies);
257
258                 if (status == count)
259                         return count;
260
261                 /* REVISIT: at HZ=100, this is sloooow */
262                 msleep(1);
263         } while (time_before(read_time, timeout));
264
265         return -ETIMEDOUT;
266 }
267
268 static ssize_t at24_read(struct at24_data *at24,
269                 char *buf, loff_t off, size_t count)
270 {
271         ssize_t retval = 0;
272
273         if (unlikely(!count))
274                 return count;
275
276         /*
277          * Read data from chip, protecting against concurrent updates
278          * from this host, but not from other I2C masters.
279          */
280         mutex_lock(&at24->lock);
281
282         while (count) {
283                 ssize_t status;
284
285                 status = at24_eeprom_read(at24, buf, off, count);
286                 if (status <= 0) {
287                         if (retval == 0)
288                                 retval = status;
289                         break;
290                 }
291                 buf += status;
292                 off += status;
293                 count -= status;
294                 retval += status;
295         }
296
297         mutex_unlock(&at24->lock);
298
299         return retval;
300 }
301
302 static ssize_t at24_bin_read(struct kobject *kobj, struct bin_attribute *attr,
303                 char *buf, loff_t off, size_t count)
304 {
305         struct at24_data *at24;
306
307         at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
308         return at24_read(at24, buf, off, count);
309 }
310
311
312 /*
313  * Note that if the hardware write-protect pin is pulled high, the whole
314  * chip is normally write protected. But there are plenty of product
315  * variants here, including OTP fuses and partial chip protect.
316  *
317  * We only use page mode writes; the alternative is sloooow. This routine
318  * writes at most one page.
319  */
320 static ssize_t at24_eeprom_write(struct at24_data *at24, const char *buf,
321                 unsigned offset, size_t count)
322 {
323         struct i2c_client *client;
324         struct i2c_msg msg;
325         ssize_t status;
326         unsigned long timeout, write_time;
327         unsigned next_page;
328
329         /* Get corresponding I2C address and adjust offset */
330         client = at24_translate_offset(at24, &offset);
331
332         /* write_max is at most a page */
333         if (count > at24->write_max)
334                 count = at24->write_max;
335
336         /* Never roll over backwards, to the start of this page */
337         next_page = roundup(offset + 1, at24->chip.page_size);
338         if (offset + count > next_page)
339                 count = next_page - offset;
340
341         /* If we'll use I2C calls for I/O, set up the message */
342         if (!at24->use_smbus) {
343                 int i = 0;
344
345                 msg.addr = client->addr;
346                 msg.flags = 0;
347
348                 /* msg.buf is u8 and casts will mask the values */
349                 msg.buf = at24->writebuf;
350                 if (at24->chip.flags & AT24_FLAG_ADDR16)
351                         msg.buf[i++] = offset >> 8;
352
353                 msg.buf[i++] = offset;
354                 memcpy(&msg.buf[i], buf, count);
355                 msg.len = i + count;
356         }
357
358         /*
359          * Writes fail if the previous one didn't complete yet. We may
360          * loop a few times until this one succeeds, waiting at least
361          * long enough for one entire page write to work.
362          */
363         timeout = jiffies + msecs_to_jiffies(write_timeout);
364         do {
365                 write_time = jiffies;
366                 if (at24->use_smbus) {
367                         status = i2c_smbus_write_i2c_block_data(client,
368                                         offset, count, buf);
369                         if (status == 0)
370                                 status = count;
371                 } else {
372                         status = i2c_transfer(client->adapter, &msg, 1);
373                         if (status == 1)
374                                 status = count;
375                 }
376                 dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n",
377                                 count, offset, status, jiffies);
378
379                 if (status == count)
380                         return count;
381
382                 /* REVISIT: at HZ=100, this is sloooow */
383                 msleep(1);
384         } while (time_before(write_time, timeout));
385
386         return -ETIMEDOUT;
387 }
388
389 static ssize_t at24_write(struct at24_data *at24, const char *buf, loff_t off,
390                           size_t count)
391 {
392         ssize_t retval = 0;
393
394         if (unlikely(!count))
395                 return count;
396
397         /*
398          * Write data to chip, protecting against concurrent updates
399          * from this host, but not from other I2C masters.
400          */
401         mutex_lock(&at24->lock);
402
403         while (count) {
404                 ssize_t status;
405
406                 status = at24_eeprom_write(at24, buf, off, count);
407                 if (status <= 0) {
408                         if (retval == 0)
409                                 retval = status;
410                         break;
411                 }
412                 buf += status;
413                 off += status;
414                 count -= status;
415                 retval += status;
416         }
417
418         mutex_unlock(&at24->lock);
419
420         return retval;
421 }
422
423 static ssize_t at24_bin_write(struct kobject *kobj, struct bin_attribute *attr,
424                 char *buf, loff_t off, size_t count)
425 {
426         struct at24_data *at24;
427
428         at24 = dev_get_drvdata(container_of(kobj, struct device, kobj));
429         return at24_write(at24, buf, off, count);
430 }
431
432 /*-------------------------------------------------------------------------*/
433
434 /*
435  * This lets other kernel code access the eeprom data. For example, it
436  * might hold a board's Ethernet address, or board-specific calibration
437  * data generated on the manufacturing floor.
438  */
439
440 static ssize_t at24_macc_read(struct memory_accessor *macc, char *buf,
441                          off_t offset, size_t count)
442 {
443         struct at24_data *at24 = container_of(macc, struct at24_data, macc);
444
445         return at24_read(at24, buf, offset, count);
446 }
447
448 static ssize_t at24_macc_write(struct memory_accessor *macc, const char *buf,
449                           off_t offset, size_t count)
450 {
451         struct at24_data *at24 = container_of(macc, struct at24_data, macc);
452
453         return at24_write(at24, buf, offset, count);
454 }
455
456 /*-------------------------------------------------------------------------*/
457
458 static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
459 {
460         struct at24_platform_data chip;
461         bool writable;
462         int use_smbus = 0;
463         struct at24_data *at24;
464         int err;
465         unsigned i, num_addresses;
466         kernel_ulong_t magic;
467
468         if (client->dev.platform_data) {
469                 chip = *(struct at24_platform_data *)client->dev.platform_data;
470         } else {
471                 if (!id->driver_data) {
472                         err = -ENODEV;
473                         goto err_out;
474                 }
475                 magic = id->driver_data;
476                 chip.byte_len = BIT(magic & AT24_BITMASK(AT24_SIZE_BYTELEN));
477                 magic >>= AT24_SIZE_BYTELEN;
478                 chip.flags = magic & AT24_BITMASK(AT24_SIZE_FLAGS);
479                 /*
480                  * This is slow, but we can't know all eeproms, so we better
481                  * play safe. Specifying custom eeprom-types via platform_data
482                  * is recommended anyhow.
483                  */
484                 chip.page_size = 1;
485
486                 chip.setup = NULL;
487                 chip.context = NULL;
488         }
489
490         if (!is_power_of_2(chip.byte_len))
491                 dev_warn(&client->dev,
492                         "byte_len looks suspicious (no power of 2)!\n");
493         if (!is_power_of_2(chip.page_size))
494                 dev_warn(&client->dev,
495                         "page_size looks suspicious (no power of 2)!\n");
496
497         /* Use I2C operations unless we're stuck with SMBus extensions. */
498         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
499                 if (chip.flags & AT24_FLAG_ADDR16) {
500                         err = -EPFNOSUPPORT;
501                         goto err_out;
502                 }
503                 if (i2c_check_functionality(client->adapter,
504                                 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
505                         use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
506                 } else if (i2c_check_functionality(client->adapter,
507                                 I2C_FUNC_SMBUS_READ_WORD_DATA)) {
508                         use_smbus = I2C_SMBUS_WORD_DATA;
509                 } else if (i2c_check_functionality(client->adapter,
510                                 I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
511                         use_smbus = I2C_SMBUS_BYTE_DATA;
512                 } else {
513                         err = -EPFNOSUPPORT;
514                         goto err_out;
515                 }
516         }
517
518         if (chip.flags & AT24_FLAG_TAKE8ADDR)
519                 num_addresses = 8;
520         else
521                 num_addresses = DIV_ROUND_UP(chip.byte_len,
522                         (chip.flags & AT24_FLAG_ADDR16) ? 65536 : 256);
523
524         at24 = kzalloc(sizeof(struct at24_data) +
525                 num_addresses * sizeof(struct i2c_client *), GFP_KERNEL);
526         if (!at24) {
527                 err = -ENOMEM;
528                 goto err_out;
529         }
530
531         mutex_init(&at24->lock);
532         at24->use_smbus = use_smbus;
533         at24->chip = chip;
534         at24->num_addresses = num_addresses;
535
536         /*
537          * Export the EEPROM bytes through sysfs, since that's convenient.
538          * By default, only root should see the data (maybe passwords etc)
539          */
540         sysfs_bin_attr_init(&at24->bin);
541         at24->bin.attr.name = "eeprom";
542         at24->bin.attr.mode = chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;
543         at24->bin.read = at24_bin_read;
544         at24->bin.size = chip.byte_len;
545
546         at24->macc.read = at24_macc_read;
547
548         writable = !(chip.flags & AT24_FLAG_READONLY);
549         if (writable) {
550                 if (!use_smbus || i2c_check_functionality(client->adapter,
551                                 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) {
552
553                         unsigned write_max = chip.page_size;
554
555                         at24->macc.write = at24_macc_write;
556
557                         at24->bin.write = at24_bin_write;
558                         at24->bin.attr.mode |= S_IWUSR;
559
560                         if (write_max > io_limit)
561                                 write_max = io_limit;
562                         if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
563                                 write_max = I2C_SMBUS_BLOCK_MAX;
564                         at24->write_max = write_max;
565
566                         /* buffer (data + address at the beginning) */
567                         at24->writebuf = kmalloc(write_max + 2, GFP_KERNEL);
568                         if (!at24->writebuf) {
569                                 err = -ENOMEM;
570                                 goto err_struct;
571                         }
572                 } else {
573                         dev_warn(&client->dev,
574                                 "cannot write due to controller restrictions.");
575                 }
576         }
577
578         at24->client[0] = client;
579
580         /* use dummy devices for multiple-address chips */
581         for (i = 1; i < num_addresses; i++) {
582                 at24->client[i] = i2c_new_dummy(client->adapter,
583                                         client->addr + i);
584                 if (!at24->client[i]) {
585                         dev_err(&client->dev, "address 0x%02x unavailable\n",
586                                         client->addr + i);
587                         err = -EADDRINUSE;
588                         goto err_clients;
589                 }
590         }
591
592         err = sysfs_create_bin_file(&client->dev.kobj, &at24->bin);
593         if (err)
594                 goto err_clients;
595
596         i2c_set_clientdata(client, at24);
597
598         dev_info(&client->dev, "%zu byte %s EEPROM %s\n",
599                 at24->bin.size, client->name,
600                 writable ? "(writable)" : "(read-only)");
601         if (use_smbus == I2C_SMBUS_WORD_DATA ||
602             use_smbus == I2C_SMBUS_BYTE_DATA) {
603                 dev_notice(&client->dev, "Falling back to %s reads, "
604                            "performance will suffer\n", use_smbus ==
605                            I2C_SMBUS_WORD_DATA ? "word" : "byte");
606         }
607         dev_dbg(&client->dev,
608                 "page_size %d, num_addresses %d, write_max %d, use_smbus %d\n",
609                 chip.page_size, num_addresses,
610                 at24->write_max, use_smbus);
611
612         /* export data to kernel code */
613         if (chip.setup)
614                 chip.setup(&at24->macc, chip.context);
615
616         return 0;
617
618 err_clients:
619         for (i = 1; i < num_addresses; i++)
620                 if (at24->client[i])
621                         i2c_unregister_device(at24->client[i]);
622
623         kfree(at24->writebuf);
624 err_struct:
625         kfree(at24);
626 err_out:
627         dev_dbg(&client->dev, "probe error %d\n", err);
628         return err;
629 }
630
631 static int __devexit at24_remove(struct i2c_client *client)
632 {
633         struct at24_data *at24;
634         int i;
635
636         at24 = i2c_get_clientdata(client);
637         sysfs_remove_bin_file(&client->dev.kobj, &at24->bin);
638
639         for (i = 1; i < at24->num_addresses; i++)
640                 i2c_unregister_device(at24->client[i]);
641
642         kfree(at24->writebuf);
643         kfree(at24);
644         i2c_set_clientdata(client, NULL);
645         return 0;
646 }
647
648 /*-------------------------------------------------------------------------*/
649
650 static struct i2c_driver at24_driver = {
651         .driver = {
652                 .name = "at24",
653                 .owner = THIS_MODULE,
654         },
655         .probe = at24_probe,
656         .remove = __devexit_p(at24_remove),
657         .id_table = at24_ids,
658 };
659
660 static int __init at24_init(void)
661 {
662         io_limit = rounddown_pow_of_two(io_limit);
663         return i2c_add_driver(&at24_driver);
664 }
665 module_init(at24_init);
666
667 static void __exit at24_exit(void)
668 {
669         i2c_del_driver(&at24_driver);
670 }
671 module_exit(at24_exit);
672
673 MODULE_DESCRIPTION("Driver for most I2C EEPROMs");
674 MODULE_AUTHOR("David Brownell and Wolfram Sang");
675 MODULE_LICENSE("GPL");