w1_ds28e17.c 18 KB

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