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