ds2482.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 /* 1-wire speed */
  50. #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
  51. #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
  52. #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
  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. MODULE_DEVICE_TABLE(i2c, ds2482_id);
  84. static struct i2c_driver ds2482_driver = {
  85. .driver = {
  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. * Helper to calculate values for configuration register
  114. * @param conf the raw config value
  115. * @return the value w/ complements that can be written to register
  116. */
  117. static inline u8 ds2482_calculate_config(u8 conf)
  118. {
  119. return conf | ((~conf & 0x0f) << 4);
  120. }
  121. /**
  122. * Sets the read pointer.
  123. * @param pdev The ds2482 client pointer
  124. * @param read_ptr see DS2482_PTR_CODE_xxx above
  125. * @return -1 on failure, 0 on success
  126. */
  127. static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
  128. {
  129. if (pdev->read_prt != read_ptr) {
  130. if (i2c_smbus_write_byte_data(pdev->client,
  131. DS2482_CMD_SET_READ_PTR,
  132. read_ptr) < 0)
  133. return -1;
  134. pdev->read_prt = read_ptr;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * Sends a command without a parameter
  140. * @param pdev The ds2482 client pointer
  141. * @param cmd DS2482_CMD_RESET,
  142. * DS2482_CMD_1WIRE_RESET,
  143. * DS2482_CMD_1WIRE_READ_BYTE
  144. * @return -1 on failure, 0 on success
  145. */
  146. static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
  147. {
  148. if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
  149. return -1;
  150. pdev->read_prt = DS2482_PTR_CODE_STATUS;
  151. return 0;
  152. }
  153. /**
  154. * Sends a command with a parameter
  155. * @param pdev The ds2482 client pointer
  156. * @param cmd DS2482_CMD_WRITE_CONFIG,
  157. * DS2482_CMD_1WIRE_SINGLE_BIT,
  158. * DS2482_CMD_1WIRE_WRITE_BYTE,
  159. * DS2482_CMD_1WIRE_TRIPLET
  160. * @param byte The data to send
  161. * @return -1 on failure, 0 on success
  162. */
  163. static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
  164. u8 cmd, u8 byte)
  165. {
  166. if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
  167. return -1;
  168. /* all cmds leave in STATUS, except CONFIG */
  169. pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
  170. DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
  171. return 0;
  172. }
  173. /*
  174. * 1-Wire interface code
  175. */
  176. #define DS2482_WAIT_IDLE_TIMEOUT 100
  177. /**
  178. * Waits until the 1-wire interface is idle (not busy)
  179. *
  180. * @param pdev Pointer to the device structure
  181. * @return the last value read from status or -1 (failure)
  182. */
  183. static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
  184. {
  185. int temp = -1;
  186. int retries = 0;
  187. if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
  188. do {
  189. temp = i2c_smbus_read_byte(pdev->client);
  190. } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
  191. (++retries < DS2482_WAIT_IDLE_TIMEOUT));
  192. }
  193. if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
  194. pr_err("%s: timeout on channel %d\n",
  195. __func__, pdev->channel);
  196. return temp;
  197. }
  198. /**
  199. * Selects a w1 channel.
  200. * The 1-wire interface must be idle before calling this function.
  201. *
  202. * @param pdev The ds2482 client pointer
  203. * @param channel 0-7
  204. * @return -1 (failure) or 0 (success)
  205. */
  206. static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
  207. {
  208. if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
  209. ds2482_chan_wr[channel]) < 0)
  210. return -1;
  211. pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
  212. pdev->channel = -1;
  213. if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
  214. pdev->channel = channel;
  215. return 0;
  216. }
  217. return -1;
  218. }
  219. /**
  220. * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
  221. *
  222. * @param data The ds2482 channel pointer
  223. * @param bit The level to write: 0 or non-zero
  224. * @return The level read: 0 or 1
  225. */
  226. static u8 ds2482_w1_touch_bit(void *data, u8 bit)
  227. {
  228. struct ds2482_w1_chan *pchan = data;
  229. struct ds2482_data *pdev = pchan->pdev;
  230. int status = -1;
  231. mutex_lock(&pdev->access_lock);
  232. /* Select the channel */
  233. ds2482_wait_1wire_idle(pdev);
  234. if (pdev->w1_count > 1)
  235. ds2482_set_channel(pdev, pchan->channel);
  236. /* Send the touch command, wait until 1WB == 0, return the status */
  237. if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
  238. bit ? 0xFF : 0))
  239. status = ds2482_wait_1wire_idle(pdev);
  240. mutex_unlock(&pdev->access_lock);
  241. return (status & DS2482_REG_STS_SBR) ? 1 : 0;
  242. }
  243. /**
  244. * Performs the triplet function, which reads two bits and writes a bit.
  245. * The bit written is determined by the two reads:
  246. * 00 => dbit, 01 => 0, 10 => 1
  247. *
  248. * @param data The ds2482 channel pointer
  249. * @param dbit The direction to choose if both branches are valid
  250. * @return b0=read1 b1=read2 b3=bit written
  251. */
  252. static u8 ds2482_w1_triplet(void *data, u8 dbit)
  253. {
  254. struct ds2482_w1_chan *pchan = data;
  255. struct ds2482_data *pdev = pchan->pdev;
  256. int status = (3 << 5);
  257. mutex_lock(&pdev->access_lock);
  258. /* Select the channel */
  259. ds2482_wait_1wire_idle(pdev);
  260. if (pdev->w1_count > 1)
  261. ds2482_set_channel(pdev, pchan->channel);
  262. /* Send the triplet command, wait until 1WB == 0, return the status */
  263. if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
  264. dbit ? 0xFF : 0))
  265. status = ds2482_wait_1wire_idle(pdev);
  266. mutex_unlock(&pdev->access_lock);
  267. /* Decode the status */
  268. return (status >> 5);
  269. }
  270. /**
  271. * Performs the write byte function.
  272. *
  273. * @param data The ds2482 channel pointer
  274. * @param byte The value to write
  275. */
  276. static void ds2482_w1_write_byte(void *data, u8 byte)
  277. {
  278. struct ds2482_w1_chan *pchan = data;
  279. struct ds2482_data *pdev = pchan->pdev;
  280. mutex_lock(&pdev->access_lock);
  281. /* Select the channel */
  282. ds2482_wait_1wire_idle(pdev);
  283. if (pdev->w1_count > 1)
  284. ds2482_set_channel(pdev, pchan->channel);
  285. /* Send the write byte command */
  286. ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
  287. mutex_unlock(&pdev->access_lock);
  288. }
  289. /**
  290. * Performs the read byte function.
  291. *
  292. * @param data The ds2482 channel pointer
  293. * @return The value read
  294. */
  295. static u8 ds2482_w1_read_byte(void *data)
  296. {
  297. struct ds2482_w1_chan *pchan = data;
  298. struct ds2482_data *pdev = pchan->pdev;
  299. int result;
  300. mutex_lock(&pdev->access_lock);
  301. /* Select the channel */
  302. ds2482_wait_1wire_idle(pdev);
  303. if (pdev->w1_count > 1)
  304. ds2482_set_channel(pdev, pchan->channel);
  305. /* Send the read byte command */
  306. ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
  307. /* Wait until 1WB == 0 */
  308. ds2482_wait_1wire_idle(pdev);
  309. /* Select the data register */
  310. ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
  311. /* Read the data byte */
  312. result = i2c_smbus_read_byte(pdev->client);
  313. mutex_unlock(&pdev->access_lock);
  314. return result;
  315. }
  316. /**
  317. * Sends a reset on the 1-wire interface
  318. *
  319. * @param data The ds2482 channel pointer
  320. * @return 0=Device present, 1=No device present or error
  321. */
  322. static u8 ds2482_w1_reset_bus(void *data)
  323. {
  324. struct ds2482_w1_chan *pchan = data;
  325. struct ds2482_data *pdev = pchan->pdev;
  326. int err;
  327. u8 retval = 1;
  328. mutex_lock(&pdev->access_lock);
  329. /* Select the channel */
  330. ds2482_wait_1wire_idle(pdev);
  331. if (pdev->w1_count > 1)
  332. ds2482_set_channel(pdev, pchan->channel);
  333. /* Send the reset command */
  334. err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
  335. if (err >= 0) {
  336. /* Wait until the reset is complete */
  337. err = ds2482_wait_1wire_idle(pdev);
  338. retval = !(err & DS2482_REG_STS_PPD);
  339. /* If the chip did reset since detect, re-config it */
  340. if (err & DS2482_REG_STS_RST)
  341. ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
  342. ds2482_calculate_config(0x00));
  343. }
  344. mutex_unlock(&pdev->access_lock);
  345. return retval;
  346. }
  347. static u8 ds2482_w1_set_pullup(void *data, int delay)
  348. {
  349. struct ds2482_w1_chan *pchan = data;
  350. struct ds2482_data *pdev = pchan->pdev;
  351. u8 retval = 1;
  352. /* if delay is non-zero activate the pullup,
  353. * the strong pullup will be automatically deactivated
  354. * by the master, so do not explicitly deactive it
  355. */
  356. if (delay) {
  357. /* both waits are crucial, otherwise devices might not be
  358. * powered long enough, causing e.g. a w1_therm sensor to
  359. * provide wrong conversion results
  360. */
  361. ds2482_wait_1wire_idle(pdev);
  362. /* note: it seems like both SPU and APU have to be set! */
  363. retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
  364. ds2482_calculate_config(DS2482_REG_CFG_SPU |
  365. DS2482_REG_CFG_APU));
  366. ds2482_wait_1wire_idle(pdev);
  367. }
  368. return retval;
  369. }
  370. static int ds2482_probe(struct i2c_client *client,
  371. const struct i2c_device_id *id)
  372. {
  373. struct ds2482_data *data;
  374. int err = -ENODEV;
  375. int temp1;
  376. int idx;
  377. if (!i2c_check_functionality(client->adapter,
  378. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  379. I2C_FUNC_SMBUS_BYTE))
  380. return -ENODEV;
  381. if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
  382. err = -ENOMEM;
  383. goto exit;
  384. }
  385. data->client = client;
  386. i2c_set_clientdata(client, data);
  387. /* Reset the device (sets the read_ptr to status) */
  388. if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
  389. dev_warn(&client->dev, "DS2482 reset failed.\n");
  390. goto exit_free;
  391. }
  392. /* Sleep at least 525ns to allow the reset to complete */
  393. ndelay(525);
  394. /* Read the status byte - only reset bit and line should be set */
  395. temp1 = i2c_smbus_read_byte(client);
  396. if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
  397. dev_warn(&client->dev, "DS2482 reset status "
  398. "0x%02X - not a DS2482\n", temp1);
  399. goto exit_free;
  400. }
  401. /* Detect the 8-port version */
  402. data->w1_count = 1;
  403. if (ds2482_set_channel(data, 7) == 0)
  404. data->w1_count = 8;
  405. /* Set all config items to 0 (off) */
  406. ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
  407. ds2482_calculate_config(0x00));
  408. mutex_init(&data->access_lock);
  409. /* Register 1-wire interface(s) */
  410. for (idx = 0; idx < data->w1_count; idx++) {
  411. data->w1_ch[idx].pdev = data;
  412. data->w1_ch[idx].channel = idx;
  413. /* Populate all the w1 bus master stuff */
  414. data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
  415. data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
  416. data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
  417. data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
  418. data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
  419. data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
  420. data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
  421. err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
  422. if (err) {
  423. data->w1_ch[idx].pdev = NULL;
  424. goto exit_w1_remove;
  425. }
  426. }
  427. return 0;
  428. exit_w1_remove:
  429. for (idx = 0; idx < data->w1_count; idx++) {
  430. if (data->w1_ch[idx].pdev != NULL)
  431. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  432. }
  433. exit_free:
  434. kfree(data);
  435. exit:
  436. return err;
  437. }
  438. static int ds2482_remove(struct i2c_client *client)
  439. {
  440. struct ds2482_data *data = i2c_get_clientdata(client);
  441. int idx;
  442. /* Unregister the 1-wire bridge(s) */
  443. for (idx = 0; idx < data->w1_count; idx++) {
  444. if (data->w1_ch[idx].pdev != NULL)
  445. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  446. }
  447. /* Free the memory */
  448. kfree(data);
  449. return 0;
  450. }
  451. module_i2c_driver(ds2482_driver);
  452. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  453. MODULE_DESCRIPTION("DS2482 driver");
  454. MODULE_LICENSE("GPL");