w1_ds28e17.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * w1_ds28e17.c - w1 family 19 (DS28E17) driver
  4. *
  5. * Copyright (c) 2016 Jan Kandziora <[email protected]>
  6. */
  7. #include <linux/crc16.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/i2c.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/uaccess.h>
  17. #define CRC16_INIT 0
  18. #include <linux/w1.h>
  19. #define W1_FAMILY_DS28E17 0x19
  20. /* Module setup. */
  21. MODULE_LICENSE("GPL v2");
  22. MODULE_AUTHOR("Jan Kandziora <[email protected]>");
  23. MODULE_DESCRIPTION("w1 family 19 driver for DS28E17, 1-wire to I2C master bridge");
  24. MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS28E17));
  25. /* Default I2C speed to be set when a DS28E17 is detected. */
  26. static int i2c_speed = 100;
  27. module_param_named(speed, i2c_speed, int, (S_IRUSR | S_IWUSR));
  28. MODULE_PARM_DESC(speed, "Default I2C speed to be set when a DS28E17 is detected");
  29. /* Default I2C stretch value to be set when a DS28E17 is detected. */
  30. static char i2c_stretch = 1;
  31. module_param_named(stretch, i2c_stretch, byte, (S_IRUSR | S_IWUSR));
  32. MODULE_PARM_DESC(stretch, "Default I2C stretch value to be set when a DS28E17 is detected");
  33. /* DS28E17 device command codes. */
  34. #define W1_F19_WRITE_DATA_WITH_STOP 0x4B
  35. #define W1_F19_WRITE_DATA_NO_STOP 0x5A
  36. #define W1_F19_WRITE_DATA_ONLY 0x69
  37. #define W1_F19_WRITE_DATA_ONLY_WITH_STOP 0x78
  38. #define W1_F19_READ_DATA_WITH_STOP 0x87
  39. #define W1_F19_WRITE_READ_DATA_WITH_STOP 0x2D
  40. #define W1_F19_WRITE_CONFIGURATION 0xD2
  41. #define W1_F19_READ_CONFIGURATION 0xE1
  42. #define W1_F19_ENABLE_SLEEP_MODE 0x1E
  43. #define W1_F19_READ_DEVICE_REVISION 0xC4
  44. /* DS28E17 status bits */
  45. #define W1_F19_STATUS_CRC 0x01
  46. #define W1_F19_STATUS_ADDRESS 0x02
  47. #define W1_F19_STATUS_START 0x08
  48. /*
  49. * Maximum number of I2C bytes to transfer within one CRC16 protected onewire
  50. * command.
  51. * */
  52. #define W1_F19_WRITE_DATA_LIMIT 255
  53. /* Maximum number of I2C bytes to read with one onewire command. */
  54. #define W1_F19_READ_DATA_LIMIT 255
  55. /* Constants for calculating the busy sleep. */
  56. #define W1_F19_BUSY_TIMEBASES { 90, 23, 10 }
  57. #define W1_F19_BUSY_GRATUITY 1000
  58. /* Number of checks for the busy flag before timeout. */
  59. #define W1_F19_BUSY_CHECKS 1000
  60. /* Slave specific data. */
  61. struct w1_f19_data {
  62. u8 speed;
  63. u8 stretch;
  64. struct i2c_adapter adapter;
  65. };
  66. /* Wait a while until the busy flag clears. */
  67. static int w1_f19_i2c_busy_wait(struct w1_slave *sl, size_t count)
  68. {
  69. const unsigned long timebases[3] = W1_F19_BUSY_TIMEBASES;
  70. struct w1_f19_data *data = sl->family_data;
  71. unsigned int checks;
  72. /* Check the busy flag first in any case.*/
  73. if (w1_touch_bit(sl->master, 1) == 0)
  74. return 0;
  75. /*
  76. * Do a generously long sleep in the beginning,
  77. * as we have to wait at least this time for all
  78. * the I2C bytes at the given speed to be transferred.
  79. */
  80. usleep_range(timebases[data->speed] * (data->stretch) * count,
  81. timebases[data->speed] * (data->stretch) * count
  82. + W1_F19_BUSY_GRATUITY);
  83. /* Now continusly check the busy flag sent by the DS28E17. */
  84. checks = W1_F19_BUSY_CHECKS;
  85. while ((checks--) > 0) {
  86. /* Return success if the busy flag is cleared. */
  87. if (w1_touch_bit(sl->master, 1) == 0)
  88. return 0;
  89. /* Wait one non-streched byte timeslot. */
  90. udelay(timebases[data->speed]);
  91. }
  92. /* Timeout. */
  93. dev_warn(&sl->dev, "busy timeout\n");
  94. return -ETIMEDOUT;
  95. }
  96. /* Utility function: result. */
  97. static size_t w1_f19_error(struct w1_slave *sl, u8 w1_buf[])
  98. {
  99. /* Warnings. */
  100. if (w1_buf[0] & W1_F19_STATUS_CRC)
  101. dev_warn(&sl->dev, "crc16 mismatch\n");
  102. if (w1_buf[0] & W1_F19_STATUS_ADDRESS)
  103. dev_warn(&sl->dev, "i2c device not responding\n");
  104. if ((w1_buf[0] & (W1_F19_STATUS_CRC | W1_F19_STATUS_ADDRESS)) == 0
  105. && w1_buf[1] != 0) {
  106. dev_warn(&sl->dev, "i2c short write, %d bytes not acknowledged\n",
  107. w1_buf[1]);
  108. }
  109. /* Check error conditions. */
  110. if (w1_buf[0] & W1_F19_STATUS_ADDRESS)
  111. return -ENXIO;
  112. if (w1_buf[0] & W1_F19_STATUS_START)
  113. return -EAGAIN;
  114. if (w1_buf[0] != 0 || w1_buf[1] != 0)
  115. return -EIO;
  116. /* All ok. */
  117. return 0;
  118. }
  119. /* Utility function: write data to I2C slave, single chunk. */
  120. static int __w1_f19_i2c_write(struct w1_slave *sl,
  121. const u8 *command, size_t command_count,
  122. const u8 *buffer, size_t count)
  123. {
  124. u16 crc;
  125. int error;
  126. u8 w1_buf[2];
  127. /* Send command and I2C data to DS28E17. */
  128. crc = crc16(CRC16_INIT, command, command_count);
  129. w1_write_block(sl->master, command, command_count);
  130. w1_buf[0] = count;
  131. crc = crc16(crc, w1_buf, 1);
  132. w1_write_8(sl->master, w1_buf[0]);
  133. crc = crc16(crc, buffer, count);
  134. w1_write_block(sl->master, buffer, count);
  135. w1_buf[0] = ~(crc & 0xFF);
  136. w1_buf[1] = ~((crc >> 8) & 0xFF);
  137. w1_write_block(sl->master, w1_buf, 2);
  138. /* Wait until busy flag clears (or timeout). */
  139. if (w1_f19_i2c_busy_wait(sl, count + 1) < 0)
  140. return -ETIMEDOUT;
  141. /* Read status from DS28E17. */
  142. w1_read_block(sl->master, w1_buf, 2);
  143. /* Check error conditions. */
  144. error = w1_f19_error(sl, w1_buf);
  145. if (error < 0)
  146. return error;
  147. /* Return number of bytes written. */
  148. return count;
  149. }
  150. /* Write data to I2C slave. */
  151. static int w1_f19_i2c_write(struct w1_slave *sl, u16 i2c_address,
  152. const u8 *buffer, size_t count, bool stop)
  153. {
  154. int result;
  155. int remaining = count;
  156. const u8 *p;
  157. u8 command[2];
  158. /* Check input. */
  159. if (count == 0)
  160. return -EOPNOTSUPP;
  161. /* Check whether we need multiple commands. */
  162. if (count <= W1_F19_WRITE_DATA_LIMIT) {
  163. /*
  164. * Small data amount. Data can be sent with
  165. * a single onewire command.
  166. */
  167. /* Send all data to DS28E17. */
  168. command[0] = (stop ? W1_F19_WRITE_DATA_WITH_STOP
  169. : W1_F19_WRITE_DATA_NO_STOP);
  170. command[1] = i2c_address << 1;
  171. result = __w1_f19_i2c_write(sl, command, 2, buffer, count);
  172. } else {
  173. /* Large data amount. Data has to be sent in multiple chunks. */
  174. /* Send first chunk to DS28E17. */
  175. p = buffer;
  176. command[0] = W1_F19_WRITE_DATA_NO_STOP;
  177. command[1] = i2c_address << 1;
  178. result = __w1_f19_i2c_write(sl, command, 2, p,
  179. W1_F19_WRITE_DATA_LIMIT);
  180. if (result < 0)
  181. return result;
  182. /* Resume to same DS28E17. */
  183. if (w1_reset_resume_command(sl->master))
  184. return -EIO;
  185. /* Next data chunk. */
  186. p += W1_F19_WRITE_DATA_LIMIT;
  187. remaining -= W1_F19_WRITE_DATA_LIMIT;
  188. while (remaining > W1_F19_WRITE_DATA_LIMIT) {
  189. /* Send intermediate chunk to DS28E17. */
  190. command[0] = W1_F19_WRITE_DATA_ONLY;
  191. result = __w1_f19_i2c_write(sl, command, 1, p,
  192. W1_F19_WRITE_DATA_LIMIT);
  193. if (result < 0)
  194. return result;
  195. /* Resume to same DS28E17. */
  196. if (w1_reset_resume_command(sl->master))
  197. return -EIO;
  198. /* Next data chunk. */
  199. p += W1_F19_WRITE_DATA_LIMIT;
  200. remaining -= W1_F19_WRITE_DATA_LIMIT;
  201. }
  202. /* Send final chunk to DS28E17. */
  203. command[0] = (stop ? W1_F19_WRITE_DATA_ONLY_WITH_STOP
  204. : W1_F19_WRITE_DATA_ONLY);
  205. result = __w1_f19_i2c_write(sl, command, 1, p, remaining);
  206. }
  207. return result;
  208. }
  209. /* Read data from I2C slave. */
  210. static int w1_f19_i2c_read(struct w1_slave *sl, u16 i2c_address,
  211. u8 *buffer, size_t count)
  212. {
  213. u16 crc;
  214. int error;
  215. u8 w1_buf[5];
  216. /* Check input. */
  217. if (count == 0)
  218. return -EOPNOTSUPP;
  219. /* Send command to DS28E17. */
  220. w1_buf[0] = W1_F19_READ_DATA_WITH_STOP;
  221. w1_buf[1] = i2c_address << 1 | 0x01;
  222. w1_buf[2] = count;
  223. crc = crc16(CRC16_INIT, w1_buf, 3);
  224. w1_buf[3] = ~(crc & 0xFF);
  225. w1_buf[4] = ~((crc >> 8) & 0xFF);
  226. w1_write_block(sl->master, w1_buf, 5);
  227. /* Wait until busy flag clears (or timeout). */
  228. if (w1_f19_i2c_busy_wait(sl, count + 1) < 0)
  229. return -ETIMEDOUT;
  230. /* Read status from DS28E17. */
  231. w1_buf[0] = w1_read_8(sl->master);
  232. w1_buf[1] = 0;
  233. /* Check error conditions. */
  234. error = w1_f19_error(sl, w1_buf);
  235. if (error < 0)
  236. return error;
  237. /* Read received I2C data from DS28E17. */
  238. return w1_read_block(sl->master, buffer, count);
  239. }
  240. /* Write to, then read data from I2C slave. */
  241. static int w1_f19_i2c_write_read(struct w1_slave *sl, u16 i2c_address,
  242. const u8 *wbuffer, size_t wcount, u8 *rbuffer, size_t rcount)
  243. {
  244. u16 crc;
  245. int error;
  246. u8 w1_buf[3];
  247. /* Check input. */
  248. if (wcount == 0 || rcount == 0)
  249. return -EOPNOTSUPP;
  250. /* Send command and I2C data to DS28E17. */
  251. w1_buf[0] = W1_F19_WRITE_READ_DATA_WITH_STOP;
  252. w1_buf[1] = i2c_address << 1;
  253. w1_buf[2] = wcount;
  254. crc = crc16(CRC16_INIT, w1_buf, 3);
  255. w1_write_block(sl->master, w1_buf, 3);
  256. crc = crc16(crc, wbuffer, wcount);
  257. w1_write_block(sl->master, wbuffer, wcount);
  258. w1_buf[0] = rcount;
  259. crc = crc16(crc, w1_buf, 1);
  260. w1_buf[1] = ~(crc & 0xFF);
  261. w1_buf[2] = ~((crc >> 8) & 0xFF);
  262. w1_write_block(sl->master, w1_buf, 3);
  263. /* Wait until busy flag clears (or timeout). */
  264. if (w1_f19_i2c_busy_wait(sl, wcount + rcount + 2) < 0)
  265. return -ETIMEDOUT;
  266. /* Read status from DS28E17. */
  267. w1_read_block(sl->master, w1_buf, 2);
  268. /* Check error conditions. */
  269. error = w1_f19_error(sl, w1_buf);
  270. if (error < 0)
  271. return error;
  272. /* Read received I2C data from DS28E17. */
  273. return w1_read_block(sl->master, rbuffer, rcount);
  274. }
  275. /* Do an I2C master transfer. */
  276. static int w1_f19_i2c_master_transfer(struct i2c_adapter *adapter,
  277. struct i2c_msg *msgs, int num)
  278. {
  279. struct w1_slave *sl = (struct w1_slave *) adapter->algo_data;
  280. int i = 0;
  281. int result = 0;
  282. /* Start onewire transaction. */
  283. mutex_lock(&sl->master->bus_mutex);
  284. /* Select DS28E17. */
  285. if (w1_reset_select_slave(sl)) {
  286. i = -EIO;
  287. goto error;
  288. }
  289. /* Loop while there are still messages to transfer. */
  290. while (i < num) {
  291. /*
  292. * Check for special case: Small write followed
  293. * by read to same I2C device.
  294. */
  295. if (i < (num-1)
  296. && msgs[i].addr == msgs[i+1].addr
  297. && !(msgs[i].flags & I2C_M_RD)
  298. && (msgs[i+1].flags & I2C_M_RD)
  299. && (msgs[i].len <= W1_F19_WRITE_DATA_LIMIT)) {
  300. /*
  301. * The DS28E17 has a combined transfer
  302. * for small write+read.
  303. */
  304. result = w1_f19_i2c_write_read(sl, msgs[i].addr,
  305. msgs[i].buf, msgs[i].len,
  306. msgs[i+1].buf, msgs[i+1].len);
  307. if (result < 0) {
  308. i = result;
  309. goto error;
  310. }
  311. /*
  312. * Check if we should interpret the read data
  313. * as a length byte. The DS28E17 unfortunately
  314. * has no read without stop, so we can just do
  315. * another simple read in that case.
  316. */
  317. if (msgs[i+1].flags & I2C_M_RECV_LEN) {
  318. result = w1_f19_i2c_read(sl, msgs[i+1].addr,
  319. &(msgs[i+1].buf[1]), msgs[i+1].buf[0]);
  320. if (result < 0) {
  321. i = result;
  322. goto error;
  323. }
  324. }
  325. /* Eat up read message, too. */
  326. i++;
  327. } else if (msgs[i].flags & I2C_M_RD) {
  328. /* Read transfer. */
  329. result = w1_f19_i2c_read(sl, msgs[i].addr,
  330. msgs[i].buf, msgs[i].len);
  331. if (result < 0) {
  332. i = result;
  333. goto error;
  334. }
  335. /*
  336. * Check if we should interpret the read data
  337. * as a length byte. The DS28E17 unfortunately
  338. * has no read without stop, so we can just do
  339. * another simple read in that case.
  340. */
  341. if (msgs[i].flags & I2C_M_RECV_LEN) {
  342. result = w1_f19_i2c_read(sl,
  343. msgs[i].addr,
  344. &(msgs[i].buf[1]),
  345. msgs[i].buf[0]);
  346. if (result < 0) {
  347. i = result;
  348. goto error;
  349. }
  350. }
  351. } else {
  352. /*
  353. * Write transfer.
  354. * Stop condition only for last
  355. * transfer.
  356. */
  357. result = w1_f19_i2c_write(sl,
  358. msgs[i].addr,
  359. msgs[i].buf,
  360. msgs[i].len,
  361. i == (num-1));
  362. if (result < 0) {
  363. i = result;
  364. goto error;
  365. }
  366. }
  367. /* Next message. */
  368. i++;
  369. /* Are there still messages to send/receive? */
  370. if (i < num) {
  371. /* Yes. Resume to same DS28E17. */
  372. if (w1_reset_resume_command(sl->master)) {
  373. i = -EIO;
  374. goto error;
  375. }
  376. }
  377. }
  378. error:
  379. /* End onewire transaction. */
  380. mutex_unlock(&sl->master->bus_mutex);
  381. /* Return number of messages processed or error. */
  382. return i;
  383. }
  384. /* Get I2C adapter functionality. */
  385. static u32 w1_f19_i2c_functionality(struct i2c_adapter *adapter)
  386. {
  387. /*
  388. * Plain I2C functions only.
  389. * SMBus is emulated by the kernel's I2C layer.
  390. * No "I2C_FUNC_SMBUS_QUICK"
  391. * No "I2C_FUNC_SMBUS_READ_BLOCK_DATA"
  392. * No "I2C_FUNC_SMBUS_BLOCK_PROC_CALL"
  393. */
  394. return I2C_FUNC_I2C |
  395. I2C_FUNC_SMBUS_BYTE |
  396. I2C_FUNC_SMBUS_BYTE_DATA |
  397. I2C_FUNC_SMBUS_WORD_DATA |
  398. I2C_FUNC_SMBUS_PROC_CALL |
  399. I2C_FUNC_SMBUS_WRITE_BLOCK_DATA |
  400. I2C_FUNC_SMBUS_I2C_BLOCK |
  401. I2C_FUNC_SMBUS_PEC;
  402. }
  403. /* I2C adapter quirks. */
  404. static const struct i2c_adapter_quirks w1_f19_i2c_adapter_quirks = {
  405. .max_read_len = W1_F19_READ_DATA_LIMIT,
  406. };
  407. /* I2C algorithm. */
  408. static const struct i2c_algorithm w1_f19_i2c_algorithm = {
  409. .master_xfer = w1_f19_i2c_master_transfer,
  410. .functionality = w1_f19_i2c_functionality,
  411. };
  412. /* Read I2C speed from DS28E17. */
  413. static int w1_f19_get_i2c_speed(struct w1_slave *sl)
  414. {
  415. struct w1_f19_data *data = sl->family_data;
  416. int result = -EIO;
  417. /* Start onewire transaction. */
  418. mutex_lock(&sl->master->bus_mutex);
  419. /* Select slave. */
  420. if (w1_reset_select_slave(sl))
  421. goto error;
  422. /* Read slave configuration byte. */
  423. w1_write_8(sl->master, W1_F19_READ_CONFIGURATION);
  424. result = w1_read_8(sl->master);
  425. if (result < 0 || result > 2) {
  426. result = -EIO;
  427. goto error;
  428. }
  429. /* Update speed in slave specific data. */
  430. data->speed = result;
  431. error:
  432. /* End onewire transaction. */
  433. mutex_unlock(&sl->master->bus_mutex);
  434. return result;
  435. }
  436. /* Set I2C speed on DS28E17. */
  437. static int __w1_f19_set_i2c_speed(struct w1_slave *sl, u8 speed)
  438. {
  439. struct w1_f19_data *data = sl->family_data;
  440. const int i2c_speeds[3] = { 100, 400, 900 };
  441. u8 w1_buf[2];
  442. /* Select slave. */
  443. if (w1_reset_select_slave(sl))
  444. return -EIO;
  445. w1_buf[0] = W1_F19_WRITE_CONFIGURATION;
  446. w1_buf[1] = speed;
  447. w1_write_block(sl->master, w1_buf, 2);
  448. /* Update speed in slave specific data. */
  449. data->speed = speed;
  450. dev_info(&sl->dev, "i2c speed set to %d kBaud\n", i2c_speeds[speed]);
  451. return 0;
  452. }
  453. static int w1_f19_set_i2c_speed(struct w1_slave *sl, u8 speed)
  454. {
  455. int result;
  456. /* Start onewire transaction. */
  457. mutex_lock(&sl->master->bus_mutex);
  458. /* Set I2C speed on DS28E17. */
  459. result = __w1_f19_set_i2c_speed(sl, speed);
  460. /* End onewire transaction. */
  461. mutex_unlock(&sl->master->bus_mutex);
  462. return result;
  463. }
  464. /* Sysfs attributes. */
  465. /* I2C speed attribute for a single chip. */
  466. static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
  467. char *buf)
  468. {
  469. struct w1_slave *sl = dev_to_w1_slave(dev);
  470. int result;
  471. /* Read current speed from slave. Updates data->speed. */
  472. result = w1_f19_get_i2c_speed(sl);
  473. if (result < 0)
  474. return result;
  475. /* Return current speed value. */
  476. return sprintf(buf, "%d\n", result);
  477. }
  478. static ssize_t speed_store(struct device *dev, struct device_attribute *attr,
  479. const char *buf, size_t count)
  480. {
  481. struct w1_slave *sl = dev_to_w1_slave(dev);
  482. int error;
  483. /* Valid values are: "100", "400", "900" */
  484. if (count < 3 || count > 4 || !buf)
  485. return -EINVAL;
  486. if (count == 4 && buf[3] != '\n')
  487. return -EINVAL;
  488. if (buf[1] != '0' || buf[2] != '0')
  489. return -EINVAL;
  490. /* Set speed on slave. */
  491. switch (buf[0]) {
  492. case '1':
  493. error = w1_f19_set_i2c_speed(sl, 0);
  494. break;
  495. case '4':
  496. error = w1_f19_set_i2c_speed(sl, 1);
  497. break;
  498. case '9':
  499. error = w1_f19_set_i2c_speed(sl, 2);
  500. break;
  501. default:
  502. return -EINVAL;
  503. }
  504. if (error < 0)
  505. return error;
  506. /* Return bytes written. */
  507. return count;
  508. }
  509. static DEVICE_ATTR_RW(speed);
  510. /* Busy stretch attribute for a single chip. */
  511. static ssize_t stretch_show(struct device *dev, struct device_attribute *attr,
  512. char *buf)
  513. {
  514. struct w1_slave *sl = dev_to_w1_slave(dev);
  515. struct w1_f19_data *data = sl->family_data;
  516. /* Return current stretch value. */
  517. return sprintf(buf, "%d\n", data->stretch);
  518. }
  519. static ssize_t stretch_store(struct device *dev, struct device_attribute *attr,
  520. const char *buf, size_t count)
  521. {
  522. struct w1_slave *sl = dev_to_w1_slave(dev);
  523. struct w1_f19_data *data = sl->family_data;
  524. /* Valid values are '1' to '9' */
  525. if (count < 1 || count > 2 || !buf)
  526. return -EINVAL;
  527. if (count == 2 && buf[1] != '\n')
  528. return -EINVAL;
  529. if (buf[0] < '1' || buf[0] > '9')
  530. return -EINVAL;
  531. /* Set busy stretch value. */
  532. data->stretch = buf[0] & 0x0F;
  533. /* Return bytes written. */
  534. return count;
  535. }
  536. static DEVICE_ATTR_RW(stretch);
  537. /* All attributes. */
  538. static struct attribute *w1_f19_attrs[] = {
  539. &dev_attr_speed.attr,
  540. &dev_attr_stretch.attr,
  541. NULL,
  542. };
  543. static const struct attribute_group w1_f19_group = {
  544. .attrs = w1_f19_attrs,
  545. };
  546. static const struct attribute_group *w1_f19_groups[] = {
  547. &w1_f19_group,
  548. NULL,
  549. };
  550. /* Slave add and remove functions. */
  551. static int w1_f19_add_slave(struct w1_slave *sl)
  552. {
  553. struct w1_f19_data *data = NULL;
  554. /* Allocate memory for slave specific data. */
  555. data = devm_kzalloc(&sl->dev, sizeof(*data), GFP_KERNEL);
  556. if (!data)
  557. return -ENOMEM;
  558. sl->family_data = data;
  559. /* Setup default I2C speed on slave. */
  560. switch (i2c_speed) {
  561. case 100:
  562. __w1_f19_set_i2c_speed(sl, 0);
  563. break;
  564. case 400:
  565. __w1_f19_set_i2c_speed(sl, 1);
  566. break;
  567. case 900:
  568. __w1_f19_set_i2c_speed(sl, 2);
  569. break;
  570. default:
  571. /*
  572. * A i2c_speed module parameter of anything else
  573. * than 100, 400, 900 means not to touch the
  574. * speed of the DS28E17.
  575. * We assume 400kBaud, the power-on value.
  576. */
  577. data->speed = 1;
  578. }
  579. /*
  580. * Setup default busy stretch
  581. * configuration for the DS28E17.
  582. */
  583. data->stretch = i2c_stretch;
  584. /* Setup I2C adapter. */
  585. data->adapter.owner = THIS_MODULE;
  586. data->adapter.algo = &w1_f19_i2c_algorithm;
  587. data->adapter.algo_data = sl;
  588. strcpy(data->adapter.name, "w1-");
  589. strcat(data->adapter.name, sl->name);
  590. data->adapter.dev.parent = &sl->dev;
  591. data->adapter.quirks = &w1_f19_i2c_adapter_quirks;
  592. return i2c_add_adapter(&data->adapter);
  593. }
  594. static void w1_f19_remove_slave(struct w1_slave *sl)
  595. {
  596. struct w1_f19_data *family_data = sl->family_data;
  597. /* Delete I2C adapter. */
  598. i2c_del_adapter(&family_data->adapter);
  599. /* Free slave specific data. */
  600. devm_kfree(&sl->dev, family_data);
  601. sl->family_data = NULL;
  602. }
  603. /* Declarations within the w1 subsystem. */
  604. static const struct w1_family_ops w1_f19_fops = {
  605. .add_slave = w1_f19_add_slave,
  606. .remove_slave = w1_f19_remove_slave,
  607. .groups = w1_f19_groups,
  608. };
  609. static struct w1_family w1_family_19 = {
  610. .fid = W1_FAMILY_DS28E17,
  611. .fops = &w1_f19_fops,
  612. };
  613. module_w1_family(w1_family_19);