w1-gpio-msm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * w1-gpio_msm - MSM GPIO w1 bus master driver
  3. *
  4. * Based on w1-gpio driver
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/w1-gpio-msm.h>
  15. #include <linux/gpio.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/err.h>
  19. #include <linux/of.h>
  20. #include <linux/delay.h>
  21. #include <linux/input.h>
  22. #include "../w1.h"
  23. #include "../w1_int.h"
  24. #include "../../gpio/gpio-msm-common.h"
  25. #define MAX_SAMPLE 30
  26. #define gpio_direction_input(gpio) __msm_gpio_set_config_direction_no_log(gpio, 1, 0)
  27. #define gpio_direction_output(gpio, val) __msm_gpio_set_config_direction_no_log(gpio, 0, val)
  28. #define gpio_set_value_msm(gpio, val) __msm_gpio_set_inout_no_log(gpio, val)
  29. #define gpio_get_value_msm(gpio) __msm_gpio_get_inout_no_log(gpio)
  30. static DEFINE_SPINLOCK(w1_gpio_msm_lock);
  31. #if defined(CONFIG_W1_FAST_CHECK)
  32. bool w1_is_resumed;
  33. #endif
  34. static int w1_delay_parm = 1;
  35. static void w1_delay(unsigned long tm)
  36. {
  37. udelay(tm * w1_delay_parm);
  38. }
  39. static void w1_gpio_write_bit(void *data, u8 bit);
  40. static u8 w1_gpio_read_bit(void *data);
  41. static u8 w1_gpio_set_pullup(void *data, int duration)
  42. {
  43. struct w1_gpio_msm_platform_data *pdata = data;
  44. unsigned long irq_flags;
  45. if (pdata->enable_external_pullup)
  46. pdata->enable_external_pullup(1);
  47. else {
  48. spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);
  49. gpio_direction_output(pdata->pin, 1);
  50. gpio_set_value_msm(pdata->pin, 1);
  51. spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);
  52. }
  53. return 0;
  54. }
  55. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  56. {
  57. struct w1_gpio_msm_platform_data *pdata = data;
  58. if (bit)
  59. gpio_direction_input(pdata->pin);
  60. else
  61. gpio_direction_output(pdata->pin, 0);
  62. }
  63. static void w1_gpio_write_bit_val(void *data, u8 bit)
  64. {
  65. struct w1_gpio_msm_platform_data *pdata = data;
  66. gpio_set_value_msm(pdata->pin, bit);
  67. }
  68. static u8 w1_gpio_read_bit_val(void *data)
  69. {
  70. struct w1_gpio_msm_platform_data *pdata = data;
  71. int ret;
  72. ret = gpio_get_value_msm(pdata->pin) ? 1 : 0;
  73. return ret;
  74. }
  75. /**
  76. * Generates a write-0 or write-1 cycle and samples the level.
  77. */
  78. static u8 w1_gpio_touch_bit(void *data, u8 bit)
  79. {
  80. if (bit) {
  81. return w1_gpio_read_bit(data);
  82. } else {
  83. w1_gpio_write_bit(data, 0);
  84. return 0;
  85. }
  86. }
  87. /**
  88. * Generates a write-0 or write-1 cycle.
  89. * Only call if dev->bus_master->touch_bit is NULL
  90. */
  91. static void w1_gpio_write_bit(void *data, u8 bit)
  92. {
  93. struct w1_gpio_msm_platform_data *pdata = data;
  94. void (*write_bit)(void *, u8);
  95. unsigned long irq_flags;
  96. if (pdata->is_open_drain) {
  97. write_bit = w1_gpio_write_bit_val;
  98. } else {
  99. write_bit = w1_gpio_write_bit_dir;
  100. }
  101. spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);
  102. if (bit) {
  103. write_bit(data, 0);
  104. write_bit(data, 1);
  105. (pdata->slave_speed == 0)? w1_delay(64) : w1_delay(10);
  106. } else {
  107. write_bit(data, 0);
  108. (pdata->slave_speed == 0)? w1_delay(60) : w1_delay(8);
  109. write_bit(data, 1);
  110. (pdata->slave_speed == 0)? w1_delay(10) : w1_delay(5);
  111. }
  112. spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);
  113. }
  114. /**
  115. * Pre-write operation, currently only supporting strong pullups.
  116. * Program the hardware for a strong pullup, if one has been requested and
  117. * the hardware supports it.
  118. *
  119. */
  120. static void w1_gpio_pre_write(void *data)
  121. {
  122. w1_gpio_set_pullup(data, 0);
  123. }
  124. /**
  125. * Post-write operation, currently only supporting strong pullups.
  126. * If a strong pullup was requested, clear it if the hardware supports
  127. * them, or execute the delay otherwise, in either case clear the request.
  128. *
  129. */
  130. static void w1_gpio_post_write(void *data)
  131. {
  132. w1_gpio_set_pullup(data, 0);
  133. }
  134. /**
  135. * Writes 8 bits.
  136. *
  137. * @param data the master device data
  138. * @param byte the byte to write
  139. */
  140. static void w1_gpio_write_8(void *data, u8 byte)
  141. {
  142. int i;
  143. w1_gpio_pre_write(data);
  144. for (i = 0; i < 8; ++i) {
  145. if (i == 7)
  146. w1_gpio_pre_write(data);
  147. // w1_gpio_write_bit(data, (byte >> i) & 0x1);
  148. w1_gpio_touch_bit(data, (byte >> i) & 0x1);
  149. }
  150. w1_gpio_post_write(data);
  151. }
  152. /**
  153. * Generates a write-1 cycle and samples the level.
  154. * Only call if dev->bus_master->touch_bit is NULL
  155. */
  156. static u8 w1_gpio_read_bit(void *data)
  157. {
  158. struct w1_gpio_msm_platform_data *pdata = data;
  159. int result;
  160. void (*write_bit)(void *, u8);
  161. unsigned long irq_flags;
  162. if (pdata->is_open_drain) {
  163. write_bit = w1_gpio_write_bit_val;
  164. } else {
  165. write_bit = w1_gpio_write_bit_dir;
  166. }
  167. spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);
  168. /* sample timing is critical here */
  169. write_bit(data, 0);
  170. write_bit(data, 1);
  171. result = w1_gpio_read_bit_val(data);
  172. (pdata->slave_speed == 0)? w1_delay(55) : w1_delay(8);
  173. spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);
  174. return result & 0x1;
  175. }
  176. /**
  177. * Does a triplet - used for searching ROM addresses.
  178. * Return bits:
  179. * bit 0 = id_bit
  180. * bit 1 = comp_bit
  181. * bit 2 = dir_taken
  182. * If both bits 0 & 1 are set, the search should be restarted.
  183. *
  184. * @param data the master device data
  185. * @param bdir the bit to write if both id_bit and comp_bit are 0
  186. * @return bit fields - see above
  187. */
  188. static u8 w1_gpio_triplet(void *data, u8 bdir)
  189. {
  190. u8 id_bit = w1_gpio_touch_bit(data, 1);
  191. u8 comp_bit = w1_gpio_touch_bit(data, 1);
  192. u8 retval;
  193. if (id_bit && comp_bit)
  194. return 0x03; /* error */
  195. if (!id_bit && !comp_bit) {
  196. /* Both bits are valid, take the direction given */
  197. retval = bdir ? 0x04 : 0;
  198. } else {
  199. /* Only one bit is valid, take that direction */
  200. bdir = id_bit;
  201. retval = id_bit ? 0x05 : 0x02;
  202. }
  203. w1_gpio_touch_bit(data, bdir);
  204. return retval;
  205. }
  206. /**
  207. * Reads 8 bits.
  208. *
  209. * @param data the master device data
  210. * @return the byte read
  211. */
  212. static u8 w1_gpio_read_8(void *data)
  213. {
  214. int i;
  215. u8 res = 0;
  216. for (i = 0; i < 8; ++i)
  217. res |= (w1_gpio_touch_bit(data,1) << i);
  218. return res;
  219. }
  220. /**
  221. * Writes a series of bytes.
  222. *
  223. * @param data the master device data
  224. * @param buf pointer to the data to write
  225. * @param len the number of bytes to write
  226. */
  227. static void w1_gpio_write_block(void *data, const u8 *buf, int len)
  228. {
  229. int i;
  230. w1_gpio_pre_write(data);
  231. for (i = 0; i < len; ++i)
  232. w1_gpio_write_8(data, buf[i]); /* calls w1_pre_write */
  233. w1_gpio_post_write(data);
  234. }
  235. /**
  236. * Reads a series of bytes.
  237. *
  238. * @param data the master device data
  239. * @param buf pointer to the buffer to fill
  240. * @param len the number of bytes to read
  241. * @return the number of bytes read
  242. */
  243. static u8 w1_gpio_read_block(void *data, u8 *buf, int len)
  244. {
  245. int i;
  246. u8 ret;
  247. for (i = 0; i < len; ++i)
  248. buf[i] = w1_gpio_read_8(data);
  249. ret = len;
  250. return ret;
  251. }
  252. /**
  253. * Issues a reset bus sequence.
  254. *
  255. * @param dev The bus master pointer
  256. * @return 0=Device present, 1=No device present or error
  257. */
  258. static u8 w1_gpio_reset_bus(void *data)
  259. {
  260. int result = 1,i;
  261. struct w1_gpio_msm_platform_data *pdata = data;
  262. void (*write_bit)(void *, u8);
  263. unsigned long irq_flags;
  264. int temp_read[MAX_SAMPLE]={'1',};
  265. int loop_cnt = 3;
  266. if (pdata->is_open_drain) {
  267. write_bit = w1_gpio_write_bit_val;
  268. } else {
  269. write_bit = w1_gpio_write_bit_dir;
  270. }
  271. spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);
  272. while (loop_cnt > 0) {
  273. write_bit(data, 0);
  274. /* minimum 48, max 80 us(In DS Documnet)
  275. * be nice and sleep, except 18b20 spec lists 960us maximum,
  276. * so until we can sleep with microsecond accuracy, spin.
  277. * Feel free to come up with some other way to give up the
  278. * cpu for such a short amount of time AND get it back in
  279. * the maximum amount of time.
  280. */
  281. (pdata->slave_speed == 0)? __const_udelay(500*UDELAY_MULT) : __const_udelay(50*UDELAY_MULT);
  282. write_bit(data, 1);
  283. (pdata->slave_speed == 0)? __const_udelay(60*UDELAY_MULT) : __const_udelay(1*UDELAY_MULT);
  284. for(i=0;i<MAX_SAMPLE;i++)
  285. temp_read[i] = gpio_get_value_msm(pdata->pin);
  286. for(i=0;i<MAX_SAMPLE;i++)
  287. result &= temp_read[i];
  288. /* minmum 70 (above) + 410 = 480 us
  289. * There aren't any timing requirements between a reset and
  290. * the following transactions. Sleeping is safe here.
  291. */
  292. /* w1_delay(410); min required time */
  293. (pdata->slave_speed == 0)? msleep(1) : __const_udelay(40*UDELAY_MULT);
  294. if (result)
  295. loop_cnt--;
  296. else
  297. break;
  298. }
  299. spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);
  300. return result;
  301. }
  302. static int hall_open(struct input_dev *input)
  303. {
  304. return 0;
  305. }
  306. static void hall_close(struct input_dev *input)
  307. {
  308. }
  309. static struct of_device_id w1_gpio_msm_dt_ids[] = {
  310. { .compatible = "w1-gpio-msm", },
  311. {}
  312. };
  313. MODULE_DEVICE_TABLE(of, w1_gpio_msm_dt_ids);
  314. static int w1_gpio_msm_probe_dt(struct platform_device *pdev)
  315. {
  316. struct w1_gpio_msm_platform_data *pdata = pdev->dev.platform_data;
  317. struct device_node *np = pdev->dev.of_node;
  318. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  319. if (!pdata)
  320. return -ENOMEM;
  321. if (of_get_property(np, "linux,open-drain", NULL))
  322. pdata->is_open_drain = 1;
  323. if (of_get_property(np, "linux,slave-speed", NULL))
  324. pdata->slave_speed= 1;
  325. pdata->pin = of_get_gpio(np, 0);
  326. pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
  327. pdev->dev.platform_data = pdata;
  328. return 0;
  329. }
  330. static int w1_gpio_msm_probe(struct platform_device *pdev)
  331. {
  332. struct w1_bus_master *master;
  333. struct w1_gpio_msm_platform_data *pdata;
  334. struct input_dev *input;
  335. int err;
  336. printk(KERN_ERR "\nw1_gpio_msm_probe start\n");
  337. if (of_have_populated_dt()) {
  338. err = w1_gpio_msm_probe_dt(pdev);
  339. if (err < 0) {
  340. dev_err(&pdev->dev, "Failed to parse DT\n");
  341. return err;
  342. }
  343. }
  344. pdata = pdev->dev.platform_data;
  345. if (!pdata) {
  346. dev_err(&pdev->dev, "No configuration data\n");
  347. return -ENXIO;
  348. }
  349. master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
  350. if (!master) {
  351. dev_err(&pdev->dev, "Out of memory\n");
  352. return -ENOMEM;
  353. }
  354. /* add for sending uevent */
  355. input = input_allocate_device();
  356. if (!input) {
  357. err = -ENODEV;
  358. goto free_master;
  359. /* need to change*/
  360. }
  361. master->input = input;
  362. input_set_drvdata(input, master);
  363. input->name = "w1";
  364. input->phys = "w1";
  365. input->dev.parent = &pdev->dev;
  366. input->evbit[0] |= BIT_MASK(EV_SW);
  367. input_set_capability(input, EV_SW, SW_W1);
  368. input->open = hall_open;
  369. input->close = hall_close;
  370. /* Enable auto repeat feature of Linux input subsystem */
  371. __set_bit(EV_REP, input->evbit);
  372. err = input_register_device(input);
  373. if(err) {
  374. dev_err(&pdev->dev, "input_register_device failed!\n");
  375. goto free_input;
  376. }
  377. spin_lock_init(&w1_gpio_msm_lock);
  378. err = gpio_request(pdata->pin, "w1");
  379. if (err) {
  380. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  381. goto free_input;
  382. }
  383. if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
  384. err = gpio_request_one(pdata->ext_pullup_enable_pin,
  385. GPIOF_INIT_LOW, "w1 pullup");
  386. if (err < 0) {
  387. dev_err(&pdev->dev, "gpio_request_one "
  388. "(ext_pullup_enable_pin) failed\n");
  389. goto free_gpio;
  390. }
  391. }
  392. master->data = pdata;
  393. master->read_bit = w1_gpio_read_bit_val;
  394. master->touch_bit = w1_gpio_touch_bit;
  395. master->read_byte = w1_gpio_read_8;
  396. master->write_byte = w1_gpio_write_8;
  397. master->read_block = w1_gpio_read_block;
  398. master->write_block = w1_gpio_write_block;
  399. master->triplet = w1_gpio_triplet;
  400. master->reset_bus = w1_gpio_reset_bus;
  401. master->set_pullup = w1_gpio_set_pullup;
  402. if (pdata->is_open_drain) {
  403. gpio_direction_output(pdata->pin, 1);
  404. master->write_bit = w1_gpio_write_bit_val;
  405. } else {
  406. gpio_direction_input(pdata->pin);
  407. master->write_bit = w1_gpio_write_bit_dir;
  408. }
  409. err = w1_add_master_device(master);
  410. if (err) {
  411. dev_err(&pdev->dev, "w1_add_master device failed\n");
  412. goto free_gpio_ext_pu;
  413. }
  414. if (pdata->enable_external_pullup)
  415. pdata->enable_external_pullup(1);
  416. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  417. gpio_set_value_msm(pdata->ext_pullup_enable_pin, 1);
  418. platform_set_drvdata(pdev, master);
  419. return 0;
  420. free_gpio_ext_pu:
  421. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  422. gpio_free(pdata->ext_pullup_enable_pin);
  423. free_gpio:
  424. gpio_free(pdata->pin);
  425. free_input:
  426. kfree(input);
  427. free_master:
  428. kfree(master);
  429. return err;
  430. }
  431. static int w1_gpio_msm_remove(struct platform_device *pdev)
  432. {
  433. struct w1_bus_master *master = platform_get_drvdata(pdev);
  434. struct w1_gpio_msm_platform_data *pdata = pdev->dev.platform_data;
  435. if (pdata->enable_external_pullup)
  436. pdata->enable_external_pullup(0);
  437. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  438. gpio_set_value_msm(pdata->ext_pullup_enable_pin, 0);
  439. w1_remove_master_device(master);
  440. gpio_free(pdata->pin);
  441. kfree(master);
  442. return 0;
  443. }
  444. #ifdef CONFIG_PM
  445. static int w1_gpio_msm_suspend(struct platform_device *pdev, pm_message_t state)
  446. {
  447. struct w1_gpio_msm_platform_data *pdata = pdev->dev.platform_data;
  448. if (pdata->enable_external_pullup)
  449. pdata->enable_external_pullup(0);
  450. gpio_tlmm_config(GPIO_CFG(pdata->pin, 0, GPIO_CFG_INPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), 1);
  451. gpio_direction_input(pdata->pin);
  452. #ifdef CONFIG_W1_WORKQUEUE
  453. cancel_delayed_work_sync(&w1_gdev->w1_dwork);
  454. #endif
  455. return 0;
  456. }
  457. static int w1_gpio_msm_resume(struct platform_device *pdev)
  458. {
  459. struct w1_gpio_msm_platform_data *pdata = pdev->dev.platform_data;
  460. if (pdata->enable_external_pullup)
  461. pdata->enable_external_pullup(1);
  462. gpio_tlmm_config(GPIO_CFG(pdata->pin, 0, GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL, GPIO_CFG_2MA), 1);
  463. gpio_direction_output(pdata->pin, 1);
  464. #if defined(CONFIG_W1_FAST_CHECK)
  465. w1_is_resumed = true;
  466. #endif
  467. #ifdef CONFIG_W1_WORKQUEUE
  468. schedule_delayed_work(&w1_gdev->w1_dwork, HZ * 2);
  469. #endif
  470. return 0;
  471. }
  472. #else
  473. #define w1_gpio_msm_suspend NULL
  474. #define w1_gpio_msm_resume NULL
  475. #endif
  476. static struct platform_driver w1_gpio_msm_driver = {
  477. .driver = {
  478. .name = "w1-gpio-msm",
  479. .owner = THIS_MODULE,
  480. .of_match_table = of_match_ptr(w1_gpio_msm_dt_ids),
  481. },
  482. .probe = w1_gpio_msm_probe,
  483. .remove = w1_gpio_msm_remove,
  484. .suspend = w1_gpio_msm_suspend,
  485. .resume = w1_gpio_msm_resume,
  486. };
  487. static int __init w1_gpio_msm_init(void)
  488. {
  489. return platform_driver_probe(&w1_gpio_msm_driver, w1_gpio_msm_probe);
  490. }
  491. static void __exit w1_gpio_msm_exit(void)
  492. {
  493. platform_driver_unregister(&w1_gpio_msm_driver);
  494. }
  495. late_initcall(w1_gpio_msm_init);
  496. module_exit(w1_gpio_msm_exit);
  497. MODULE_DESCRIPTION("MSM GPIO w1 bus master driver");
  498. MODULE_AUTHOR("clark.kim@maximintegrated.com");
  499. MODULE_LICENSE("GPL");