max6875.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. max6875.c - driver for MAX6874/MAX6875
  3. Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  4. Based on i2c/chips/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. /* Do not scan - the MAX6875 access method will write to some EEPROM chips */
  27. static unsigned short normal_i2c[] = {I2C_CLIENT_END};
  28. /* Insmod parameters */
  29. I2C_CLIENT_INSMOD_1(max6875);
  30. /* The MAX6875 can only read/write 16 bytes at a time */
  31. #define SLICE_SIZE 16
  32. #define SLICE_BITS 4
  33. /* USER EEPROM is at addresses 0x8100 - 0x82FF */
  34. #define USER_EEPROM_BASE 0x8100
  35. #define USER_EEPROM_SIZE 0x0200
  36. #define USER_EEPROM_SLICES 32
  37. /* MAX6875 commands */
  38. #define MAX6875_CMD_BLK_READ 0x84
  39. /* Each client has this additional data */
  40. struct max6875_data {
  41. struct i2c_client client;
  42. struct mutex update_lock;
  43. u32 valid;
  44. u8 data[USER_EEPROM_SIZE];
  45. unsigned long last_updated[USER_EEPROM_SLICES];
  46. };
  47. static int max6875_attach_adapter(struct i2c_adapter *adapter);
  48. static int max6875_detect(struct i2c_adapter *adapter, int address, int kind);
  49. static int max6875_detach_client(struct i2c_client *client);
  50. /* This is the driver that will be inserted */
  51. static struct i2c_driver max6875_driver = {
  52. .driver = {
  53. .name = "max6875",
  54. },
  55. .attach_adapter = max6875_attach_adapter,
  56. .detach_client = max6875_detach_client,
  57. };
  58. static void max6875_update_slice(struct i2c_client *client, int slice)
  59. {
  60. struct max6875_data *data = i2c_get_clientdata(client);
  61. int i, j, addr;
  62. u8 *buf;
  63. if (slice >= USER_EEPROM_SLICES)
  64. return;
  65. mutex_lock(&data->update_lock);
  66. buf = &data->data[slice << SLICE_BITS];
  67. if (!(data->valid & (1 << slice)) ||
  68. time_after(jiffies, data->last_updated[slice])) {
  69. dev_dbg(&client->dev, "Starting update of slice %u\n", slice);
  70. data->valid &= ~(1 << slice);
  71. addr = USER_EEPROM_BASE + (slice << SLICE_BITS);
  72. /* select the eeprom address */
  73. if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) {
  74. dev_err(&client->dev, "address set failed\n");
  75. goto exit_up;
  76. }
  77. if (i2c_check_functionality(client->adapter,
  78. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  79. if (i2c_smbus_read_i2c_block_data(client,
  80. MAX6875_CMD_BLK_READ,
  81. SLICE_SIZE,
  82. buf) != SLICE_SIZE) {
  83. goto exit_up;
  84. }
  85. } else {
  86. for (i = 0; i < SLICE_SIZE; i++) {
  87. j = i2c_smbus_read_byte(client);
  88. if (j < 0) {
  89. goto exit_up;
  90. }
  91. buf[i] = j;
  92. }
  93. }
  94. data->last_updated[slice] = jiffies;
  95. data->valid |= (1 << slice);
  96. }
  97. exit_up:
  98. mutex_unlock(&data->update_lock);
  99. }
  100. static ssize_t max6875_read(struct kobject *kobj,
  101. struct bin_attribute *bin_attr,
  102. char *buf, loff_t off, size_t count)
  103. {
  104. struct i2c_client *client = kobj_to_i2c_client(kobj);
  105. struct max6875_data *data = i2c_get_clientdata(client);
  106. int slice, max_slice;
  107. if (off > USER_EEPROM_SIZE)
  108. return 0;
  109. if (off + count > USER_EEPROM_SIZE)
  110. count = USER_EEPROM_SIZE - off;
  111. /* refresh slices which contain requested bytes */
  112. max_slice = (off + count - 1) >> SLICE_BITS;
  113. for (slice = (off >> SLICE_BITS); slice <= max_slice; slice++)
  114. max6875_update_slice(client, slice);
  115. memcpy(buf, &data->data[off], count);
  116. return count;
  117. }
  118. static struct bin_attribute user_eeprom_attr = {
  119. .attr = {
  120. .name = "eeprom",
  121. .mode = S_IRUGO,
  122. },
  123. .size = USER_EEPROM_SIZE,
  124. .read = max6875_read,
  125. };
  126. static int max6875_attach_adapter(struct i2c_adapter *adapter)
  127. {
  128. return i2c_probe(adapter, &addr_data, max6875_detect);
  129. }
  130. /* This function is called by i2c_probe */
  131. static int max6875_detect(struct i2c_adapter *adapter, int address, int kind)
  132. {
  133. struct i2c_client *real_client;
  134. struct i2c_client *fake_client;
  135. struct max6875_data *data;
  136. int err = 0;
  137. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE_DATA
  138. | I2C_FUNC_SMBUS_READ_BYTE))
  139. return 0;
  140. /* Only check even addresses */
  141. if (address & 1)
  142. return 0;
  143. if (!(data = kzalloc(sizeof(struct max6875_data), GFP_KERNEL)))
  144. return -ENOMEM;
  145. /* A fake client is created on the odd address */
  146. if (!(fake_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  147. err = -ENOMEM;
  148. goto exit_kfree1;
  149. }
  150. /* Init real i2c_client */
  151. real_client = &data->client;
  152. i2c_set_clientdata(real_client, data);
  153. real_client->addr = address;
  154. real_client->adapter = adapter;
  155. real_client->driver = &max6875_driver;
  156. real_client->flags = 0;
  157. strlcpy(real_client->name, "max6875", I2C_NAME_SIZE);
  158. mutex_init(&data->update_lock);
  159. /* Init fake client data */
  160. i2c_set_clientdata(fake_client, NULL);
  161. fake_client->addr = address | 1;
  162. fake_client->adapter = adapter;
  163. fake_client->driver = &max6875_driver;
  164. fake_client->flags = 0;
  165. strlcpy(fake_client->name, "max6875 subclient", I2C_NAME_SIZE);
  166. /* Prevent 24RF08 corruption (in case of user error) */
  167. i2c_smbus_write_quick(real_client, 0);
  168. if ((err = i2c_attach_client(real_client)) != 0)
  169. goto exit_kfree2;
  170. if ((err = i2c_attach_client(fake_client)) != 0)
  171. goto exit_detach1;
  172. err = sysfs_create_bin_file(&real_client->dev.kobj, &user_eeprom_attr);
  173. if (err)
  174. goto exit_detach2;
  175. return 0;
  176. exit_detach2:
  177. i2c_detach_client(fake_client);
  178. exit_detach1:
  179. i2c_detach_client(real_client);
  180. exit_kfree2:
  181. kfree(fake_client);
  182. exit_kfree1:
  183. kfree(data);
  184. return err;
  185. }
  186. /* Will be called for both the real client and the fake client */
  187. static int max6875_detach_client(struct i2c_client *client)
  188. {
  189. int err;
  190. struct max6875_data *data = i2c_get_clientdata(client);
  191. /* data is NULL for the fake client */
  192. if (data)
  193. sysfs_remove_bin_file(&client->dev.kobj, &user_eeprom_attr);
  194. err = i2c_detach_client(client);
  195. if (err)
  196. return err;
  197. if (data) /* real client */
  198. kfree(data);
  199. else /* fake client */
  200. kfree(client);
  201. return 0;
  202. }
  203. static int __init max6875_init(void)
  204. {
  205. return i2c_add_driver(&max6875_driver);
  206. }
  207. static void __exit max6875_exit(void)
  208. {
  209. i2c_del_driver(&max6875_driver);
  210. }
  211. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  212. MODULE_DESCRIPTION("MAX6875 driver");
  213. MODULE_LICENSE("GPL");
  214. module_init(max6875_init);
  215. module_exit(max6875_exit);