ds2482.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 <bgardner@wabtec.com>
  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. * https://www.analog.com/en/products/ds2482-100.html
  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 <linux/regulator/consumer.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, 0644);
  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] = { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
  73. static const u8 ds2482_chan_rd[8] = { 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. /*
  86. * Client data (each client gets its own)
  87. */
  88. struct ds2482_data;
  89. struct ds2482_w1_chan {
  90. struct ds2482_data *pdev;
  91. u8 channel;
  92. struct w1_bus_master w1_bm;
  93. };
  94. struct ds2482_data {
  95. struct i2c_client *client;
  96. struct mutex access_lock;
  97. /* 1-wire interface(s) */
  98. int w1_count; /* 1 or 8 */
  99. struct ds2482_w1_chan w1_ch[8];
  100. /* per-device values */
  101. u8 channel;
  102. u8 read_prt; /* see DS2482_PTR_CODE_xxx */
  103. u8 reg_config;
  104. };
  105. /**
  106. * ds2482_calculate_config - Helper to calculate values for configuration register
  107. * @conf: the raw config value
  108. * Return: the value w/ complements that can be written to register
  109. */
  110. static inline u8 ds2482_calculate_config(u8 conf)
  111. {
  112. conf |= extra_config;
  113. if (ds2482_active_pullup)
  114. conf |= DS2482_REG_CFG_APU;
  115. return conf | ((~conf & 0x0f) << 4);
  116. }
  117. /**
  118. * ds2482_select_register - Sets the read pointer.
  119. * @pdev: The ds2482 client pointer
  120. * @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. * ds2482_send_cmd - Sends a command without a parameter
  136. * @pdev: The ds2482 client pointer
  137. * @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. * ds2482_send_cmd_data - Sends a command with a parameter
  151. * @pdev: The ds2482 client pointer
  152. * @cmd: DS2482_CMD_WRITE_CONFIG,
  153. * DS2482_CMD_1WIRE_SINGLE_BIT,
  154. * DS2482_CMD_1WIRE_WRITE_BYTE,
  155. * DS2482_CMD_1WIRE_TRIPLET
  156. * @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. * ds2482_wait_1wire_idle - Waits until the 1-wire interface is idle (not busy)
  175. *
  176. * @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. * ds2482_set_channel - Selects a w1 channel.
  196. * The 1-wire interface must be idle before calling this function.
  197. *
  198. * @pdev: The ds2482 client pointer
  199. * @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. * ds2482_w1_touch_bit - Performs the touch-bit function, which writes a 0 or 1 and reads the level.
  217. *
  218. * @data: The ds2482 channel pointer
  219. * @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. * ds2482_w1_triplet - 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. * @data: The ds2482 channel pointer
  245. * @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. * ds2482_w1_write_byte - Performs the write byte function.
  268. *
  269. * @data: The ds2482 channel pointer
  270. * @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. * ds2482_w1_read_byte - Performs the read byte function.
  287. *
  288. * @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. * ds2482_w1_reset_bus - Sends a reset on the 1-wire interface
  314. *
  315. * @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. {
  368. struct ds2482_data *data;
  369. int err = -ENODEV;
  370. int temp1;
  371. int idx;
  372. int ret;
  373. if (!i2c_check_functionality(client->adapter,
  374. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  375. I2C_FUNC_SMBUS_BYTE))
  376. return -ENODEV;
  377. data = devm_kzalloc(&client->dev, sizeof(struct ds2482_data), GFP_KERNEL);
  378. if (!data)
  379. return -ENOMEM;
  380. ret = devm_regulator_get_enable(&client->dev, "vcc");
  381. if (ret)
  382. return dev_err_probe(&client->dev, ret, "Failed to enable regulator\n");
  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. return err;
  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. return err;
  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. return err;
  432. }
  433. static void ds2482_remove(struct i2c_client *client)
  434. {
  435. struct ds2482_data *data = i2c_get_clientdata(client);
  436. int idx;
  437. /* Unregister the 1-wire bridge(s) */
  438. for (idx = 0; idx < data->w1_count; idx++) {
  439. if (data->w1_ch[idx].pdev != NULL)
  440. w1_remove_master_device(&data->w1_ch[idx].w1_bm);
  441. }
  442. }
  443. /*
  444. * Driver data (common to all clients)
  445. */
  446. static const struct i2c_device_id ds2482_id[] = {
  447. { "ds2482" },
  448. { "ds2484" },
  449. { }
  450. };
  451. MODULE_DEVICE_TABLE(i2c, ds2482_id);
  452. static struct i2c_driver ds2482_driver = {
  453. .driver = {
  454. .name = "ds2482",
  455. },
  456. .probe = ds2482_probe,
  457. .remove = ds2482_remove,
  458. .id_table = ds2482_id,
  459. };
  460. module_i2c_driver(ds2482_driver);
  461. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  462. MODULE_DESCRIPTION("DS2482 driver");
  463. MODULE_LICENSE("GPL");