max6875.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 two EEPROM sections: config and user.
  6. At reset, the config EEPROM is read into the registers.
  7. This driver make 3 binary files available in sysfs:
  8. reg_config - direct access to the registers
  9. eeprom_config - acesses configuration eeprom space
  10. eeprom_user - free for application use
  11. In our application, we put device serial & model numbers in user eeprom.
  12. Notes:
  13. 1) The datasheet says that register 0x44 / EEPROM 0x8044 should NOT
  14. be overwritten, so the driver explicitly prevents that.
  15. 2) It's a good idea to keep the config (0x45) locked in config EEPROM.
  16. You can temporarily enable config writes by changing register 0x45.
  17. This program is free software; you can redistribute it and/or modify
  18. it under the terms of the GNU General Public License as published by
  19. the Free Software Foundation; version 2 of the License.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/sched.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-sensor.h>
  30. /* Addresses to scan */
  31. /* No address scanned by default, as this could corrupt standard EEPROMS. */
  32. static unsigned short normal_i2c[] = {I2C_CLIENT_END};
  33. static unsigned int normal_isa[] = {I2C_CLIENT_ISA_END};
  34. /* Insmod parameters */
  35. SENSORS_INSMOD_1(max6875);
  36. /* this param will prevent 'accidental' writes to the eeprom */
  37. static int allow_write = 0;
  38. module_param(allow_write, int, 0);
  39. MODULE_PARM_DESC(allow_write,
  40. "Enable write access:\n"
  41. "*0: Read only\n"
  42. " 1: Read/Write access");
  43. /* The MAX6875 can only read/write 16 bytes at a time */
  44. #define SLICE_SIZE 16
  45. #define SLICE_BITS 4
  46. /* CONFIG EEPROM is at addresses 0x8000 - 0x8045, registers are at 0 - 0x45 */
  47. #define CONFIG_EEPROM_BASE 0x8000
  48. #define CONFIG_EEPROM_SIZE 0x0046
  49. #define CONFIG_EEPROM_SLICES 5
  50. /* USER EEPROM is at addresses 0x8100 - 0x82FF */
  51. #define USER_EEPROM_BASE 0x8100
  52. #define USER_EEPROM_SIZE 0x0200
  53. #define USER_EEPROM_SLICES 32
  54. /* MAX6875 commands */
  55. #define MAX6875_CMD_BLOCK_WRITE 0x83
  56. #define MAX6875_CMD_BLOCK_READ 0x84
  57. #define MAX6875_CMD_REBOOT 0x88
  58. enum max6875_area_type {
  59. max6875_register_config=0,
  60. max6875_eeprom_config,
  61. max6875_eeprom_user,
  62. max6857_max
  63. };
  64. struct eeprom_block {
  65. enum max6875_area_type type;
  66. u8 slices;
  67. u32 size;
  68. u32 valid;
  69. u32 base;
  70. unsigned long *updated;
  71. u8 *data;
  72. };
  73. /* Each client has this additional data */
  74. struct max6875_data {
  75. struct i2c_client client;
  76. struct semaphore update_lock;
  77. struct eeprom_block blocks[max6857_max];
  78. /* the above structs point into the arrays below */
  79. u8 data[USER_EEPROM_SIZE + (CONFIG_EEPROM_SIZE*2)];
  80. unsigned long last_updated[USER_EEPROM_SLICES + (CONFIG_EEPROM_SLICES*2)];
  81. };
  82. static int max6875_attach_adapter(struct i2c_adapter *adapter);
  83. static int max6875_detect(struct i2c_adapter *adapter, int address, int kind);
  84. static int max6875_detach_client(struct i2c_client *client);
  85. /* This is the driver that will be inserted */
  86. static struct i2c_driver max6875_driver = {
  87. .owner = THIS_MODULE,
  88. .name = "max6875",
  89. .flags = I2C_DF_NOTIFY,
  90. .attach_adapter = max6875_attach_adapter,
  91. .detach_client = max6875_detach_client,
  92. };
  93. static int max6875_update_slice(struct i2c_client *client,
  94. struct eeprom_block *blk,
  95. int slice)
  96. {
  97. struct max6875_data *data = i2c_get_clientdata(client);
  98. int i, j, addr, count;
  99. u8 rdbuf[SLICE_SIZE];
  100. int retval = 0;
  101. if (slice >= blk->slices)
  102. return -1;
  103. down(&data->update_lock);
  104. if (!(blk->valid & (1 << slice)) ||
  105. (jiffies - blk->updated[slice] > 300 * HZ) ||
  106. (jiffies < blk->updated[slice])) {
  107. dev_dbg(&client->dev, "Starting eeprom update, slice %u, base %u\n",
  108. slice, blk->base);
  109. addr = blk->base + (slice << SLICE_BITS);
  110. count = blk->size - (slice << SLICE_BITS);
  111. if (count > SLICE_SIZE) {
  112. count = SLICE_SIZE;
  113. }
  114. /* Preset the read address */
  115. if (addr < 0x100) {
  116. /* select the register */
  117. if (i2c_smbus_write_byte(client, addr & 0xFF)) {
  118. dev_dbg(&client->dev, "max6875 register select has failed!\n");
  119. retval = -1;
  120. goto exit;
  121. }
  122. } else {
  123. /* select the eeprom */
  124. if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) {
  125. dev_dbg(&client->dev, "max6875 address set has failed!\n");
  126. retval = -1;
  127. goto exit;
  128. }
  129. }
  130. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  131. if (i2c_smbus_read_i2c_block_data(client, MAX6875_CMD_BLOCK_READ,
  132. rdbuf) != SLICE_SIZE)
  133. {
  134. retval = -1;
  135. goto exit;
  136. }
  137. memcpy(&blk->data[slice << SLICE_BITS], rdbuf, count);
  138. } else {
  139. for (i = 0; i < count; i++) {
  140. j = i2c_smbus_read_byte(client);
  141. if (j < 0)
  142. {
  143. retval = -1;
  144. goto exit;
  145. }
  146. blk->data[(slice << SLICE_BITS) + i] = (u8) j;
  147. }
  148. }
  149. blk->updated[slice] = jiffies;
  150. blk->valid |= (1 << slice);
  151. }
  152. exit:
  153. up(&data->update_lock);
  154. return retval;
  155. }
  156. static ssize_t max6875_read(struct kobject *kobj, char *buf, loff_t off, size_t count,
  157. enum max6875_area_type area_type)
  158. {
  159. struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
  160. struct max6875_data *data = i2c_get_clientdata(client);
  161. struct eeprom_block *blk;
  162. int slice;
  163. blk = &data->blocks[area_type];
  164. if (off > blk->size)
  165. return 0;
  166. if (off + count > blk->size)
  167. count = blk->size - off;
  168. /* Only refresh slices which contain requested bytes */
  169. for (slice = (off >> SLICE_BITS); slice <= ((off + count - 1) >> SLICE_BITS); slice++)
  170. max6875_update_slice(client, blk, slice);
  171. memcpy(buf, &blk->data[off], count);
  172. return count;
  173. }
  174. static ssize_t max6875_user_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
  175. {
  176. return max6875_read(kobj, buf, off, count, max6875_eeprom_user);
  177. }
  178. static ssize_t max6875_config_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
  179. {
  180. return max6875_read(kobj, buf, off, count, max6875_eeprom_config);
  181. }
  182. static ssize_t max6875_cfgreg_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
  183. {
  184. return max6875_read(kobj, buf, off, count, max6875_register_config);
  185. }
  186. static ssize_t max6875_write(struct kobject *kobj, char *buf, loff_t off, size_t count,
  187. enum max6875_area_type area_type)
  188. {
  189. struct i2c_client *client = to_i2c_client(container_of(kobj, struct device, kobj));
  190. struct max6875_data *data = i2c_get_clientdata(client);
  191. struct eeprom_block *blk;
  192. int slice, addr, retval;
  193. ssize_t sent = 0;
  194. blk = &data->blocks[area_type];
  195. if (off > blk->size)
  196. return 0;
  197. if ((off + count) > blk->size)
  198. count = blk->size - off;
  199. if (down_interruptible(&data->update_lock))
  200. return -EAGAIN;
  201. /* writing to a register is done with i2c_smbus_write_byte_data() */
  202. if (blk->type == max6875_register_config) {
  203. for (sent = 0; sent < count; sent++) {
  204. addr = off + sent;
  205. if (addr == 0x44)
  206. continue;
  207. retval = i2c_smbus_write_byte_data(client, addr, buf[sent]);
  208. }
  209. } else {
  210. int cmd, val;
  211. /* We are writing to EEPROM */
  212. for (sent = 0; sent < count; sent++) {
  213. addr = blk->base + off + sent;
  214. cmd = addr >> 8;
  215. val = (addr & 0xff) | (buf[sent] << 8); // reversed
  216. if (addr == 0x8044)
  217. continue;
  218. retval = i2c_smbus_write_word_data(client, cmd, val);
  219. if (retval) {
  220. goto error_exit;
  221. }
  222. /* A write takes up to 11 ms */
  223. msleep(11);
  224. }
  225. }
  226. /* Invalidate the scratch buffer */
  227. for (slice = (off >> SLICE_BITS); slice <= ((off + count - 1) >> SLICE_BITS); slice++)
  228. blk->valid &= ~(1 << slice);
  229. error_exit:
  230. up(&data->update_lock);
  231. return sent;
  232. }
  233. static ssize_t max6875_user_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
  234. {
  235. return max6875_write(kobj, buf, off, count, max6875_eeprom_user);
  236. }
  237. static ssize_t max6875_config_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
  238. {
  239. return max6875_write(kobj, buf, off, count, max6875_eeprom_config);
  240. }
  241. static ssize_t max6875_cfgreg_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
  242. {
  243. return max6875_write(kobj, buf, off, count, max6875_register_config);
  244. }
  245. static struct bin_attribute user_eeprom_attr = {
  246. .attr = {
  247. .name = "eeprom_user",
  248. .mode = S_IRUGO | S_IWUSR | S_IWGRP,
  249. .owner = THIS_MODULE,
  250. },
  251. .size = USER_EEPROM_SIZE,
  252. .read = max6875_user_read,
  253. .write = max6875_user_write,
  254. };
  255. static struct bin_attribute config_eeprom_attr = {
  256. .attr = {
  257. .name = "eeprom_config",
  258. .mode = S_IRUGO | S_IWUSR,
  259. .owner = THIS_MODULE,
  260. },
  261. .size = CONFIG_EEPROM_SIZE,
  262. .read = max6875_config_read,
  263. .write = max6875_config_write,
  264. };
  265. static struct bin_attribute config_register_attr = {
  266. .attr = {
  267. .name = "reg_config",
  268. .mode = S_IRUGO | S_IWUSR,
  269. .owner = THIS_MODULE,
  270. },
  271. .size = CONFIG_EEPROM_SIZE,
  272. .read = max6875_cfgreg_read,
  273. .write = max6875_cfgreg_write,
  274. };
  275. static int max6875_attach_adapter(struct i2c_adapter *adapter)
  276. {
  277. return i2c_detect(adapter, &addr_data, max6875_detect);
  278. }
  279. /* This function is called by i2c_detect */
  280. static int max6875_detect(struct i2c_adapter *adapter, int address, int kind)
  281. {
  282. struct i2c_client *new_client;
  283. struct max6875_data *data;
  284. int err = 0;
  285. /* Prevent 24RF08 corruption (in case of user error) */
  286. if (kind < 0)
  287. i2c_smbus_xfer(adapter, address, 0, 0, 0,
  288. I2C_SMBUS_QUICK, NULL);
  289. /* There are three ways we can read the EEPROM data:
  290. (1) I2C block reads (faster, but unsupported by most adapters)
  291. (2) Consecutive byte reads (100% overhead)
  292. (3) Regular byte data reads (200% overhead)
  293. The third method is not implemented by this driver because all
  294. known adapters support at least the second. */
  295. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
  296. I2C_FUNC_SMBUS_BYTE |
  297. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  298. goto exit;
  299. /* OK. For now, we presume we have a valid client. We now create the
  300. client structure, even though we cannot fill it completely yet.
  301. But it allows us to access eeprom_{read,write}_value. */
  302. if (!(data = kmalloc(sizeof(struct max6875_data), GFP_KERNEL))) {
  303. err = -ENOMEM;
  304. goto exit;
  305. }
  306. memset(data, 0, sizeof(struct max6875_data));
  307. new_client = &data->client;
  308. i2c_set_clientdata(new_client, data);
  309. new_client->addr = address;
  310. new_client->adapter = adapter;
  311. new_client->driver = &max6875_driver;
  312. new_client->flags = 0;
  313. /* Setup the user section */
  314. data->blocks[max6875_eeprom_user].type = max6875_eeprom_user;
  315. data->blocks[max6875_eeprom_user].slices = USER_EEPROM_SLICES;
  316. data->blocks[max6875_eeprom_user].size = USER_EEPROM_SIZE;
  317. data->blocks[max6875_eeprom_user].base = USER_EEPROM_BASE;
  318. data->blocks[max6875_eeprom_user].data = data->data;
  319. data->blocks[max6875_eeprom_user].updated = data->last_updated;
  320. /* Setup the config section */
  321. data->blocks[max6875_eeprom_config].type = max6875_eeprom_config;
  322. data->blocks[max6875_eeprom_config].slices = CONFIG_EEPROM_SLICES;
  323. data->blocks[max6875_eeprom_config].size = CONFIG_EEPROM_SIZE;
  324. data->blocks[max6875_eeprom_config].base = CONFIG_EEPROM_BASE;
  325. data->blocks[max6875_eeprom_config].data = &data->data[USER_EEPROM_SIZE];
  326. data->blocks[max6875_eeprom_config].updated = &data->last_updated[USER_EEPROM_SLICES];
  327. /* Setup the register section */
  328. data->blocks[max6875_register_config].type = max6875_register_config;
  329. data->blocks[max6875_register_config].slices = CONFIG_EEPROM_SLICES;
  330. data->blocks[max6875_register_config].size = CONFIG_EEPROM_SIZE;
  331. data->blocks[max6875_register_config].base = 0;
  332. data->blocks[max6875_register_config].data = &data->data[USER_EEPROM_SIZE+CONFIG_EEPROM_SIZE];
  333. data->blocks[max6875_register_config].updated = &data->last_updated[USER_EEPROM_SLICES+CONFIG_EEPROM_SLICES];
  334. /* Init the data */
  335. memset(data->data, 0xff, sizeof(data->data));
  336. /* Fill in the remaining client fields */
  337. strlcpy(new_client->name, "max6875", I2C_NAME_SIZE);
  338. init_MUTEX(&data->update_lock);
  339. /* Verify that the chip is really what we think it is */
  340. if ((max6875_update_slice(new_client, &data->blocks[max6875_eeprom_config], 4) < 0) ||
  341. (max6875_update_slice(new_client, &data->blocks[max6875_register_config], 4) < 0))
  342. goto exit_kfree;
  343. /* 0x41,0x42 must be zero and 0x40 must match in eeprom and registers */
  344. if ((data->blocks[max6875_eeprom_config].data[0x41] != 0) ||
  345. (data->blocks[max6875_eeprom_config].data[0x42] != 0) ||
  346. (data->blocks[max6875_register_config].data[0x41] != 0) ||
  347. (data->blocks[max6875_register_config].data[0x42] != 0) ||
  348. (data->blocks[max6875_eeprom_config].data[0x40] !=
  349. data->blocks[max6875_register_config].data[0x40]))
  350. goto exit_kfree;
  351. /* Tell the I2C layer a new client has arrived */
  352. if ((err = i2c_attach_client(new_client)))
  353. goto exit_kfree;
  354. /* create the sysfs eeprom files with the correct permissions */
  355. if (allow_write == 0) {
  356. user_eeprom_attr.attr.mode &= ~S_IWUGO;
  357. user_eeprom_attr.write = NULL;
  358. config_eeprom_attr.attr.mode &= ~S_IWUGO;
  359. config_eeprom_attr.write = NULL;
  360. config_register_attr.attr.mode &= ~S_IWUGO;
  361. config_register_attr.write = NULL;
  362. }
  363. sysfs_create_bin_file(&new_client->dev.kobj, &user_eeprom_attr);
  364. sysfs_create_bin_file(&new_client->dev.kobj, &config_eeprom_attr);
  365. sysfs_create_bin_file(&new_client->dev.kobj, &config_register_attr);
  366. return 0;
  367. exit_kfree:
  368. kfree(data);
  369. exit:
  370. return err;
  371. }
  372. static int max6875_detach_client(struct i2c_client *client)
  373. {
  374. int err;
  375. err = i2c_detach_client(client);
  376. if (err) {
  377. dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
  378. return err;
  379. }
  380. kfree(i2c_get_clientdata(client));
  381. return 0;
  382. }
  383. static int __init max6875_init(void)
  384. {
  385. return i2c_add_driver(&max6875_driver);
  386. }
  387. static void __exit max6875_exit(void)
  388. {
  389. i2c_del_driver(&max6875_driver);
  390. }
  391. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  392. MODULE_DESCRIPTION("MAX6875 driver");
  393. MODULE_LICENSE("GPL");
  394. module_init(max6875_init);
  395. module_exit(max6875_exit);