ds2482.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /**
  2. * ds2482.c - provides i2c to w1-master bridge(s)
  3. * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  4. *
  5. * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
  6. * It is a I2C to 1-wire bridge.
  7. * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
  8. * The complete datasheet can be obtained from MAXIM's website at:
  9. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/delay.h>
  20. #include <asm/delay.h>
  21. #include "../w1.h"
  22. #include "../w1_int.h"
  23. /**
  24. * The DS2482 registers - there are 3 registers that are addressed by a read
  25. * pointer. The read pointer is set by the last command executed.
  26. *
  27. * To read the data, issue a register read for any address
  28. */
  29. #define DS2482_CMD_RESET 0xF0 /* No param */
  30. #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
  31. #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
  32. #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
  33. #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
  34. #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
  35. #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
  36. #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
  37. /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
  38. #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
  39. /* Values for DS2482_CMD_SET_READ_PTR */
  40. #define DS2482_PTR_CODE_STATUS 0xF0
  41. #define DS2482_PTR_CODE_DATA 0xE1
  42. #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
  43. #define DS2482_PTR_CODE_CONFIG 0xC3
  44. /**
  45. * Configure Register bit definitions
  46. * The top 4 bits always read 0.
  47. * To write, the top nibble must be the 1's compl. of the low nibble.
  48. */
  49. #define DS2482_REG_CFG_1WS 0x08
  50. #define DS2482_REG_CFG_SPU 0x04
  51. #define DS2482_REG_CFG_PPM 0x02
  52. #define DS2482_REG_CFG_APU 0x01
  53. /**
  54. * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
  55. * To set the channel, write the value at the index of the channel.
  56. * Read and compare against the corresponding value to verify the change.
  57. */
  58. static const u8 ds2482_chan_wr[8] =
  59. { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
  60. static const u8 ds2482_chan_rd[8] =
  61. { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
  62. /**
  63. * Status Register bit definitions (read only)
  64. */
  65. #define DS2482_REG_STS_DIR 0x80
  66. #define DS2482_REG_STS_TSB 0x40
  67. #define DS2482_REG_STS_SBR 0x20
  68. #define DS2482_REG_STS_RST 0x10
  69. #define DS2482_REG_STS_LL 0x08
  70. #define DS2482_REG_STS_SD 0x04
  71. #define DS2482_REG_STS_PPD 0x02
  72. #define DS2482_REG_STS_1WB 0x01
  73. static int ds2482_probe(struct i2c_client *client,
  74. const struct i2c_device_id *id);
  75. static int ds2482_remove(struct i2c_client *client);
  76. /**
  77. * Driver data (common to all clients)
  78. */
  79. static const struct i2c_device_id ds2482_id[] = {
  80. { "ds2482", 0 },
  81. { }
  82. };
  83. static struct i2c_driver ds2482_driver = {
  84. .driver = {
  85. .owner = THIS_MODULE,
  86. .name = "ds2482",
  87. },
  88. .probe = ds2482_probe,
  89. .remove = ds2482_remove,
  90. .id_table = ds2482_id,
  91. };
  92. /*
  93. * Client data (each client gets its own)
  94. */
  95. struct ds2482_data;
  96. struct ds2482_w1_chan {
  97. struct ds2482_data *pdev;
  98. u8 channel;
  99. struct w1_bus_master w1_bm;
  100. };
  101. struct ds2482_data {
  102. struct i2c_client *client;
  103. struct mutex access_lock;
  104. /* 1-wire interface(s) */
  105. int w1_count; /* 1 or 8 */
  106. struct ds2482_w1_chan w1_ch[8];
  107. /* per-device values */
  108. u8 channel;
  109. u8 read_prt; /* see DS2482_PTR_CODE_xxx */
  110. u8 reg_config;
  111. };
  112. /**
  113. * Sets the read pointer.
  114. * @param pdev The ds2482 client pointer
  115. * @param read_ptr see DS2482_PTR_CODE_xxx above
  116. * @return -1 on failure, 0 on success
  117. */
  118. static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
  119. {
  120. if (pdev->read_prt != read_ptr) {
  121. if (i2c_smbus_write_byte_data(pdev->client,
  122. DS2482_CMD_SET_READ_PTR,
  123. read_ptr) < 0)
  124. return -1;
  125. pdev->read_prt = read_ptr;
  126. }
  127. return 0;
  128. }
  129. /**
  130. * Sends a command without a parameter
  131. * @param pdev The ds2482 client pointer
  132. * @param cmd DS2482_CMD_RESET,
  133. * DS2482_CMD_1WIRE_RESET,
  134. * DS2482_CMD_1WIRE_READ_BYTE
  135. * @return -1 on failure, 0 on success
  136. */
  137. static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
  138. {
  139. if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
  140. return -1;
  141. pdev->read_prt = DS2482_PTR_CODE_STATUS;
  142. return 0;
  143. }
  144. /**
  145. * Sends a command with a parameter
  146. * @param pdev The ds2482 client pointer
  147. * @param cmd DS2482_CMD_WRITE_CONFIG,
  148. * DS2482_CMD_1WIRE_SINGLE_BIT,
  149. * DS2482_CMD_1WIRE_WRITE_BYTE,
  150. * DS2482_CMD_1WIRE_TRIPLET
  151. * @param byte The data to send
  152. * @return -1 on failure, 0 on success
  153. */
  154. static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
  155. u8 cmd, u8 byte)
  156. {
  157. if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
  158. return -1;
  159. /* all cmds leave in STATUS, except CONFIG */
  160. pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
  161. DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
  162. return 0;
  163. }
  164. /*
  165. * 1-Wire interface code
  166. */
  167. #define DS2482_WAIT_IDLE_TIMEOUT 100
  168. /**
  169. * Waits until the 1-wire interface is idle (not busy)
  170. *
  171. * @param pdev Pointer to the device structure
  172. * @return the last value read from status or -1 (failure)
  173. */
  174. static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
  175. {
  176. int temp = -1;
  177. int retries = 0;
  178. if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
  179. do {
  180. temp = i2c_smbus_read_byte(pdev->client);
  181. } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
  182. (++retries < DS2482_WAIT_IDLE_TIMEOUT));
  183. }
  184. if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
  185. printk(KERN_ERR "%s: timeout on channel %d\n",
  186. __func__, pdev->channel);
  187. return temp;
  188. }
  189. /**
  190. * Selects a w1 channel.
  191. * The 1-wire interface must be idle before calling this function.
  192. *
  193. * @param pdev The ds2482 client pointer
  194. * @param channel 0-7
  195. * @return -1 (failure) or 0 (success)
  196. */
  197. static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
  198. {
  199. if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
  200. ds2482_chan_wr[channel]) < 0)
  201. return -1;
  202. pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
  203. pdev->channel = -1;
  204. if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
  205. pdev->channel = channel;
  206. return 0;
  207. }
  208. return -1;
  209. }
  210. /**
  211. * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
  212. *
  213. * @param data The ds2482 channel pointer
  214. * @param bit The level to write: 0 or non-zero
  215. * @return The level read: 0 or 1
  216. */
  217. static u8 ds2482_w1_touch_bit(void *data, u8 bit)
  218. {
  219. struct ds2482_w1_chan *pchan = data;
  220. struct ds2482_data *pdev = pchan->pdev;
  221. int status = -1;
  222. mutex_lock(&pdev->access_lock);
  223. /* Select the channel */
  224. ds2482_wait_1wire_idle(pdev);
  225. if (pdev->w1_count > 1)
  226. ds2482_set_channel(pdev, pchan->channel);
  227. /* Send the touch command, wait until 1WB == 0, return the status */
  228. if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
  229. bit ? 0xFF : 0))
  230. status = ds2482_wait_1wire_idle(pdev);
  231. mutex_unlock(&pdev->access_lock);
  232. return (status & DS2482_REG_STS_SBR) ? 1 : 0;
  233. }
  234. /**
  235. * Performs the triplet function, which reads two bits and writes a bit.
  236. * The bit written is determined by the two reads:
  237. * 00 => dbit, 01 => 0, 10 => 1
  238. *
  239. * @param data The ds2482 channel pointer
  240. * @param dbit The direction to choose if both branches are valid
  241. * @return b0=read1 b1=read2 b3=bit written
  242. */
  243. static u8 ds2482_w1_triplet(void *data, u8 dbit)
  244. {
  245. struct ds2482_w1_chan *pchan = data;
  246. struct ds2482_data *pdev = pchan->pdev;
  247. int status = (3 << 5);
  248. mutex_lock(&pdev->access_lock);
  249. /* Select the channel */
  250. ds2482_wait_1wire_idle(pdev);
  251. if (pdev->w1_count > 1)
  252. ds2482_set_channel(pdev, pchan->channel);
  253. /* Send the triplet command, wait until 1WB == 0, return the status */
  254. if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
  255. dbit ? 0xFF : 0))
  256. status = ds2482_wait_1wire_idle(pdev);
  257. mutex_unlock(&pdev->access_lock);
  258. /* Decode the status */
  259. return (status >> 5);
  260. }
  261. /**
  262. * Performs the write byte function.
  263. *
  264. * @param data The ds2482 channel pointer
  265. * @param byte The value to write
  266. */
  267. static void ds2482_w1_write_byte(void *data, u8 byte)
  268. {
  269. struct ds2482_w1_chan *pchan = data;
  270. struct ds2482_data *pdev = pchan->pdev;
  271. mutex_lock(&pdev->access_lock);
  272. /* Select the channel */
  273. ds2482_wait_1wire_idle(pdev);
  274. if (pdev->w1_count > 1)
  275. ds2482_set_channel(pdev, pchan->channel);
  276. /* Send the write byte command */
  277. ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
  278. mutex_unlock(&pdev->access_lock);
  279. }
  280. /**
  281. * Performs the read byte function.
  282. *
  283. * @param data The ds2482 channel pointer
  284. * @return The value read
  285. */
  286. static u8 ds2482_w1_read_byte(void *data)
  287. {
  288. struct ds2482_w1_chan *pchan = data;
  289. struct ds2482_data *pdev = pchan->pdev;
  290. int result;
  291. mutex_lock(&pdev->access_lock);
  292. /* Select the channel */
  293. ds2482_wait_1wire_idle(pdev);
  294. if (pdev->w1_count > 1)
  295. ds2482_set_channel(pdev, pchan->channel);
  296. /* Send the read byte command */
  297. ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
  298. /* Wait until 1WB == 0 */
  299. ds2482_wait_1wire_idle(pdev);
  300. /* Select the data register */
  301. ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
  302. /* Read the data byte */
  303. result = i2c_smbus_read_byte(pdev->client);
  304. mutex_unlock(&pdev->access_lock);
  305. return result;
  306. }
  307. /**
  308. * Sends a reset on the 1-wire interface
  309. *
  310. * @param data The ds2482 channel pointer
  311. * @return 0=Device present, 1=No device present or error
  312. */
  313. static u8 ds2482_w1_reset_bus(void *data)
  314. {
  315. struct ds2482_w1_chan *pchan = data;
  316. struct ds2482_data *pdev = pchan->pdev;
  317. int err;
  318. u8 retval = 1;
  319. mutex_lock(&pdev->access_lock);
  320. /* Select the channel */
  321. ds2482_wait_1wire_idle(pdev);
  322. if (pdev->w1_count > 1)
  323. ds2482_set_channel(pdev, pchan->channel);
  324. /* Send the reset command */
  325. err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
  326. if (err >= 0) {
  327. /* Wait until the reset is complete */
  328. err = ds2482_wait_1wire_idle(pdev);
  329. retval = !(err & DS2482_REG_STS_PPD);
  330. /* If the chip did reset since detect, re-config it */
  331. if (err & DS2482_REG_STS_RST)
  332. ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
  333. 0xF0);
  334. }
  335. mutex_unlock(&pdev->access_lock);
  336. return retval;
  337. }
  338. static int ds2482_probe(struct i2c_client *client,
  339. const struct i2c_device_id *id)
  340. {
  341. struct ds2482_data *data;
  342. int err = -ENODEV;
  343. int temp1;
  344. int idx;
  345. if (!i2c_check_functionality(client->adapter,
  346. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  347. I2C_FUNC_SMBUS_BYTE))
  348. return -ENODEV;
  349. if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
  350. err = -ENOMEM;
  351. goto exit;
  352. }
  353. data->client = client;
  354. i2c_set_clientdata(client, data);
  355. /* Reset the device (sets the read_ptr to status) */
  356. if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
  357. dev_warn(&client->dev, "DS2482 reset failed.\n");
  358. goto exit_free;
  359. }
  360. /* Sleep at least 525ns to allow the reset to complete */
  361. ndelay(525);
  362. /* Read the status byte - only reset bit and line should be set */
  363. temp1 = i2c_smbus_read_byte(client);
  364. if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
  365. dev_warn(&client->dev, "DS2482 reset status "
  366. "0x%02X - not a DS2482\n", temp1);
  367. goto exit_free;
  368. }
  369. /* Detect the 8-port version */
  370. data->w1_count = 1;
  371. if (ds2482_set_channel(data, 7) == 0)
  372. data->w1_count = 8;
  373. /* Set all config items to 0 (off) */
  374. ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 0xF0);
  375. mutex_init(&data->access_lock);
  376. /* Register 1-wire interface(s) */
  377. for (idx = 0; idx < data->w1_count; idx++) {
  378. data->w1_ch[idx].pdev = data;
  379. data->w1_ch[idx].channel = idx;
  380. /* Populate all the w1 bus master stuff */
  381. data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
  382. data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
  383. data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
  384. data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
  385. data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
  386. data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
  387. err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
  388. if (err) {
  389. data->w1_ch[idx].pdev = NULL;
  390. goto exit_w1_remove;
  391. }
  392. }
  393. return 0;
  394. exit_w1_remove:
  395. for (idx = 0; idx < data->w1_count; idx++) {
  396. if (data->w1_ch[idx].pdev != NULL)
  397. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  398. }
  399. exit_free:
  400. kfree(data);
  401. exit:
  402. return err;
  403. }
  404. static int ds2482_remove(struct i2c_client *client)
  405. {
  406. struct ds2482_data *data = i2c_get_clientdata(client);
  407. int idx;
  408. /* Unregister the 1-wire bridge(s) */
  409. for (idx = 0; idx < data->w1_count; idx++) {
  410. if (data->w1_ch[idx].pdev != NULL)
  411. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  412. }
  413. /* Free the memory */
  414. kfree(data);
  415. return 0;
  416. }
  417. static int __init sensors_ds2482_init(void)
  418. {
  419. return i2c_add_driver(&ds2482_driver);
  420. }
  421. static void __exit sensors_ds2482_exit(void)
  422. {
  423. i2c_del_driver(&ds2482_driver);
  424. }
  425. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  426. MODULE_DESCRIPTION("DS2482 driver");
  427. MODULE_LICENSE("GPL");
  428. module_init(sensors_ds2482_init);
  429. module_exit(sensors_ds2482_exit);