max6875.c 7.0 KB

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