ds2482.c 15 KB

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