ds2482.c 15 KB

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