ds2482.c 15 KB

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