ds2482.c 14 KB

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