ds2482.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ds2482.c - provides i2c to w1-master bridge(s)
  4. * Copyright (C) 2005 Ben Gardner <[email protected]>
  5. *
  6. * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
  7. * It is a I2C to 1-wire bridge.
  8. * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
  9. * The complete datasheet can be obtained from MAXIM's website at:
  10. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/delay.h>
  17. #include <asm/delay.h>
  18. #include <linux/w1.h>
  19. /*
  20. * Allow the active pullup to be disabled, default is enabled.
  21. *
  22. * Note from the DS2482 datasheet:
  23. * The APU bit controls whether an active pullup (controlled slew-rate
  24. * transistor) or a passive pullup (Rwpu resistor) will be used to drive
  25. * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
  26. * (resistor mode). Active Pullup should always be selected unless there is
  27. * only a single slave on the 1-Wire line.
  28. */
  29. static int ds2482_active_pullup = 1;
  30. module_param_named(active_pullup, ds2482_active_pullup, int, 0644);
  31. MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \
  32. "0-disable, 1-enable (default)");
  33. /* extra configurations - e.g. 1WS */
  34. static int extra_config;
  35. module_param(extra_config, int, S_IRUGO | S_IWUSR);
  36. MODULE_PARM_DESC(extra_config, "Extra Configuration settings 1=APU,2=PPM,3=SPU,8=1WS");
  37. /*
  38. * The DS2482 registers - there are 3 registers that are addressed by a read
  39. * pointer. The read pointer is set by the last command executed.
  40. *
  41. * To read the data, issue a register read for any address
  42. */
  43. #define DS2482_CMD_RESET 0xF0 /* No param */
  44. #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
  45. #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
  46. #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
  47. #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
  48. #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
  49. #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
  50. #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
  51. /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
  52. #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
  53. /* Values for DS2482_CMD_SET_READ_PTR */
  54. #define DS2482_PTR_CODE_STATUS 0xF0
  55. #define DS2482_PTR_CODE_DATA 0xE1
  56. #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
  57. #define DS2482_PTR_CODE_CONFIG 0xC3
  58. /*
  59. * Configure Register bit definitions
  60. * The top 4 bits always read 0.
  61. * To write, the top nibble must be the 1's compl. of the low nibble.
  62. */
  63. #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */
  64. #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
  65. #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
  66. #define DS2482_REG_CFG_APU 0x01 /* active pull-up */
  67. /*
  68. * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
  69. * To set the channel, write the value at the index of the channel.
  70. * Read and compare against the corresponding value to verify the change.
  71. */
  72. static const u8 ds2482_chan_wr[8] =
  73. { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
  74. static const u8 ds2482_chan_rd[8] =
  75. { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
  76. /*
  77. * Status Register bit definitions (read only)
  78. */
  79. #define DS2482_REG_STS_DIR 0x80
  80. #define DS2482_REG_STS_TSB 0x40
  81. #define DS2482_REG_STS_SBR 0x20
  82. #define DS2482_REG_STS_RST 0x10
  83. #define DS2482_REG_STS_LL 0x08
  84. #define DS2482_REG_STS_SD 0x04
  85. #define DS2482_REG_STS_PPD 0x02
  86. #define DS2482_REG_STS_1WB 0x01
  87. /*
  88. * Client data (each client gets its own)
  89. */
  90. struct ds2482_data;
  91. struct ds2482_w1_chan {
  92. struct ds2482_data *pdev;
  93. u8 channel;
  94. struct w1_bus_master w1_bm;
  95. };
  96. struct ds2482_data {
  97. struct i2c_client *client;
  98. struct mutex access_lock;
  99. /* 1-wire interface(s) */
  100. int w1_count; /* 1 or 8 */
  101. struct ds2482_w1_chan w1_ch[8];
  102. /* per-device values */
  103. u8 channel;
  104. u8 read_prt; /* see DS2482_PTR_CODE_xxx */
  105. u8 reg_config;
  106. };
  107. /**
  108. * ds2482_calculate_config - Helper to calculate values for configuration register
  109. * @conf: the raw config value
  110. * Return: the value w/ complements that can be written to register
  111. */
  112. static inline u8 ds2482_calculate_config(u8 conf)
  113. {
  114. conf |= extra_config;
  115. if (ds2482_active_pullup)
  116. conf |= DS2482_REG_CFG_APU;
  117. return conf | ((~conf & 0x0f) << 4);
  118. }
  119. /**
  120. * ds2482_select_register - Sets the read pointer.
  121. * @pdev: The ds2482 client pointer
  122. * @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. * ds2482_send_cmd - Sends a command without a parameter
  138. * @pdev: The ds2482 client pointer
  139. * @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. * ds2482_send_cmd_data - Sends a command with a parameter
  153. * @pdev: The ds2482 client pointer
  154. * @cmd: DS2482_CMD_WRITE_CONFIG,
  155. * DS2482_CMD_1WIRE_SINGLE_BIT,
  156. * DS2482_CMD_1WIRE_WRITE_BYTE,
  157. * DS2482_CMD_1WIRE_TRIPLET
  158. * @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. * ds2482_wait_1wire_idle - Waits until the 1-wire interface is idle (not busy)
  177. *
  178. * @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. * ds2482_set_channel - Selects a w1 channel.
  198. * The 1-wire interface must be idle before calling this function.
  199. *
  200. * @pdev: The ds2482 client pointer
  201. * @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. * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level.
  219. *
  220. * @data: The ds2482 channel pointer
  221. * @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. * ds2482_w1_triplet - 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. * @data: The ds2482 channel pointer
  247. * @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. * ds2482_w1_write_byte - Performs the write byte function.
  270. *
  271. * @data: The ds2482 channel pointer
  272. * @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. * ds2482_w1_read_byte - Performs the read byte function.
  289. *
  290. * @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. * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface
  316. *
  317. * @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(0x00));
  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(DS2482_REG_CFG_SPU |
  363. DS2482_REG_CFG_APU));
  364. ds2482_wait_1wire_idle(pdev);
  365. }
  366. return retval;
  367. }
  368. static int ds2482_probe(struct i2c_client *client,
  369. const struct i2c_device_id *id)
  370. {
  371. struct ds2482_data *data;
  372. int err = -ENODEV;
  373. int temp1;
  374. int idx;
  375. if (!i2c_check_functionality(client->adapter,
  376. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  377. I2C_FUNC_SMBUS_BYTE))
  378. return -ENODEV;
  379. if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
  380. err = -ENOMEM;
  381. goto exit;
  382. }
  383. data->client = client;
  384. i2c_set_clientdata(client, data);
  385. /* Reset the device (sets the read_ptr to status) */
  386. if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
  387. dev_warn(&client->dev, "DS2482 reset failed.\n");
  388. goto exit_free;
  389. }
  390. /* Sleep at least 525ns to allow the reset to complete */
  391. ndelay(525);
  392. /* Read the status byte - only reset bit and line should be set */
  393. temp1 = i2c_smbus_read_byte(client);
  394. if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
  395. dev_warn(&client->dev, "DS2482 reset status "
  396. "0x%02X - not a DS2482\n", temp1);
  397. goto exit_free;
  398. }
  399. /* Detect the 8-port version */
  400. data->w1_count = 1;
  401. if (ds2482_set_channel(data, 7) == 0)
  402. data->w1_count = 8;
  403. /* Set all config items to 0 (off) */
  404. ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
  405. ds2482_calculate_config(0x00));
  406. mutex_init(&data->access_lock);
  407. /* Register 1-wire interface(s) */
  408. for (idx = 0; idx < data->w1_count; idx++) {
  409. data->w1_ch[idx].pdev = data;
  410. data->w1_ch[idx].channel = idx;
  411. /* Populate all the w1 bus master stuff */
  412. data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
  413. data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
  414. data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
  415. data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
  416. data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
  417. data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
  418. data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
  419. err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
  420. if (err) {
  421. data->w1_ch[idx].pdev = NULL;
  422. goto exit_w1_remove;
  423. }
  424. }
  425. return 0;
  426. exit_w1_remove:
  427. for (idx = 0; idx < data->w1_count; idx++) {
  428. if (data->w1_ch[idx].pdev != NULL)
  429. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  430. }
  431. exit_free:
  432. kfree(data);
  433. exit:
  434. return err;
  435. }
  436. static void ds2482_remove(struct i2c_client *client)
  437. {
  438. struct ds2482_data *data = i2c_get_clientdata(client);
  439. int idx;
  440. /* Unregister the 1-wire bridge(s) */
  441. for (idx = 0; idx < data->w1_count; idx++) {
  442. if (data->w1_ch[idx].pdev != NULL)
  443. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  444. }
  445. /* Free the memory */
  446. kfree(data);
  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 <[email protected]>");
  466. MODULE_DESCRIPTION("DS2482 driver");
  467. MODULE_LICENSE("GPL");