max6875.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. max6875.c - driver for MAX6874/MAX6875
  3. Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  4. Based on eeprom.c
  5. The MAX6875 has a bank of registers and two banks of EEPROM.
  6. Address ranges are defined as follows:
  7. * 0x0000 - 0x0046 = configuration registers
  8. * 0x8000 - 0x8046 = configuration EEPROM
  9. * 0x8100 - 0x82FF = user EEPROM
  10. This driver makes the user EEPROM available for read.
  11. The registers & config EEPROM should be accessed via i2c-dev.
  12. The MAX6875 ignores the lowest address bit, so each chip responds to
  13. two addresses - 0x50/0x51 and 0x52/0x53.
  14. Note that the MAX6875 uses i2c_smbus_write_byte_data() to set the read
  15. address, so this driver is destructive if loaded for the wrong EEPROM chip.
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; version 2 of the License.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. /* The MAX6875 can only read/write 16 bytes at a time */
  27. #define SLICE_SIZE 16
  28. #define SLICE_BITS 4
  29. /* USER EEPROM is at addresses 0x8100 - 0x82FF */
  30. #define USER_EEPROM_BASE 0x8100
  31. #define USER_EEPROM_SIZE 0x0200
  32. #define USER_EEPROM_SLICES 32
  33. /* MAX6875 commands */
  34. #define MAX6875_CMD_BLK_READ 0x84
  35. /* Each client has this additional data */
  36. struct max6875_data {
  37. struct i2c_client *fake_client;
  38. struct mutex update_lock;
  39. u32 valid;
  40. u8 data[USER_EEPROM_SIZE];
  41. unsigned long last_updated[USER_EEPROM_SLICES];
  42. };
  43. static void max6875_update_slice(struct i2c_client *client, int slice)
  44. {
  45. struct max6875_data *data = i2c_get_clientdata(client);
  46. int i, j, addr;
  47. u8 *buf;
  48. if (slice >= USER_EEPROM_SLICES)
  49. return;
  50. mutex_lock(&data->update_lock);
  51. buf = &data->data[slice << SLICE_BITS];
  52. if (!(data->valid & (1 << slice)) ||
  53. time_after(jiffies, data->last_updated[slice])) {
  54. dev_dbg(&client->dev, "Starting update of slice %u\n", slice);
  55. data->valid &= ~(1 << slice);
  56. addr = USER_EEPROM_BASE + (slice << SLICE_BITS);
  57. /* select the eeprom address */
  58. if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) {
  59. dev_err(&client->dev, "address set failed\n");
  60. goto exit_up;
  61. }
  62. if (i2c_check_functionality(client->adapter,
  63. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  64. if (i2c_smbus_read_i2c_block_data(client,
  65. MAX6875_CMD_BLK_READ,
  66. SLICE_SIZE,
  67. buf) != SLICE_SIZE) {
  68. goto exit_up;
  69. }
  70. } else {
  71. for (i = 0; i < SLICE_SIZE; i++) {
  72. j = i2c_smbus_read_byte(client);
  73. if (j < 0) {
  74. goto exit_up;
  75. }
  76. buf[i] = j;
  77. }
  78. }
  79. data->last_updated[slice] = jiffies;
  80. data->valid |= (1 << slice);
  81. }
  82. exit_up:
  83. mutex_unlock(&data->update_lock);
  84. }
  85. static ssize_t max6875_read(struct kobject *kobj,
  86. struct bin_attribute *bin_attr,
  87. char *buf, loff_t off, size_t count)
  88. {
  89. struct i2c_client *client = kobj_to_i2c_client(kobj);
  90. struct max6875_data *data = i2c_get_clientdata(client);
  91. int slice, max_slice;
  92. if (off > USER_EEPROM_SIZE)
  93. return 0;
  94. if (off + count > USER_EEPROM_SIZE)
  95. count = USER_EEPROM_SIZE - off;
  96. /* refresh slices which contain requested bytes */
  97. max_slice = (off + count - 1) >> SLICE_BITS;
  98. for (slice = (off >> SLICE_BITS); slice <= max_slice; slice++)
  99. max6875_update_slice(client, slice);
  100. memcpy(buf, &data->data[off], count);
  101. return count;
  102. }
  103. static struct bin_attribute user_eeprom_attr = {
  104. .attr = {
  105. .name = "eeprom",
  106. .mode = S_IRUGO,
  107. },
  108. .size = USER_EEPROM_SIZE,
  109. .read = max6875_read,
  110. };
  111. static int max6875_probe(struct i2c_client *client,
  112. const struct i2c_device_id *id)
  113. {
  114. struct i2c_adapter *adapter = client->adapter;
  115. struct max6875_data *data;
  116. int err;
  117. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE_DATA
  118. | I2C_FUNC_SMBUS_READ_BYTE))
  119. return -ENODEV;
  120. /* Only bind to even addresses */
  121. if (client->addr & 1)
  122. return -ENODEV;
  123. if (!(data = kzalloc(sizeof(struct max6875_data), GFP_KERNEL)))
  124. return -ENOMEM;
  125. /* A fake client is created on the odd address */
  126. data->fake_client = i2c_new_dummy(client->adapter, client->addr + 1);
  127. if (!data->fake_client) {
  128. err = -ENOMEM;
  129. goto exit_kfree;
  130. }
  131. /* Init real i2c_client */
  132. i2c_set_clientdata(client, data);
  133. mutex_init(&data->update_lock);
  134. err = sysfs_create_bin_file(&client->dev.kobj, &user_eeprom_attr);
  135. if (err)
  136. goto exit_remove_fake;
  137. return 0;
  138. exit_remove_fake:
  139. i2c_unregister_device(data->fake_client);
  140. exit_kfree:
  141. kfree(data);
  142. return err;
  143. }
  144. static int max6875_remove(struct i2c_client *client)
  145. {
  146. struct max6875_data *data = i2c_get_clientdata(client);
  147. i2c_unregister_device(data->fake_client);
  148. sysfs_remove_bin_file(&client->dev.kobj, &user_eeprom_attr);
  149. kfree(data);
  150. return 0;
  151. }
  152. static const struct i2c_device_id max6875_id[] = {
  153. { "max6875", 0 },
  154. { }
  155. };
  156. static struct i2c_driver max6875_driver = {
  157. .driver = {
  158. .name = "max6875",
  159. },
  160. .probe = max6875_probe,
  161. .remove = max6875_remove,
  162. .id_table = max6875_id,
  163. };
  164. static int __init max6875_init(void)
  165. {
  166. return i2c_add_driver(&max6875_driver);
  167. }
  168. static void __exit max6875_exit(void)
  169. {
  170. i2c_del_driver(&max6875_driver);
  171. }
  172. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  173. MODULE_DESCRIPTION("MAX6875 driver");
  174. MODULE_LICENSE("GPL");
  175. module_init(max6875_init);
  176. module_exit(max6875_exit);