ds2482.c 14 KB

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