max6875.c 14 KB

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