ds1wm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * 1-wire busmaster driver for DS1WM and ASICs with embedded DS1WMs
  3. * such as HP iPAQs (including h5xxx, h2200, and devices with ASIC3
  4. * like hx4700).
  5. *
  6. * Copyright (c) 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
  7. * Copyright (c) 2004-2007, Matt Reimer <mreimer@vpop.net>
  8. *
  9. * Use consistent with the GNU GPL is permitted,
  10. * provided that this copyright notice is
  11. * preserved in its entirety in all copies and derived works.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/pm.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/ds1wm.h>
  21. #include <asm/io.h>
  22. #include "../w1.h"
  23. #include "../w1_int.h"
  24. #define DS1WM_CMD 0x00 /* R/W 4 bits command */
  25. #define DS1WM_DATA 0x01 /* R/W 8 bits, transmit/receive buffer */
  26. #define DS1WM_INT 0x02 /* R/W interrupt status */
  27. #define DS1WM_INT_EN 0x03 /* R/W interrupt enable */
  28. #define DS1WM_CLKDIV 0x04 /* R/W 5 bits of divisor and pre-scale */
  29. #define DS1WM_CMD_1W_RESET (1 << 0) /* force reset on 1-wire bus */
  30. #define DS1WM_CMD_SRA (1 << 1) /* enable Search ROM accelerator mode */
  31. #define DS1WM_CMD_DQ_OUTPUT (1 << 2) /* write only - forces bus low */
  32. #define DS1WM_CMD_DQ_INPUT (1 << 3) /* read only - reflects state of bus */
  33. #define DS1WM_CMD_RST (1 << 5) /* software reset */
  34. #define DS1WM_CMD_OD (1 << 7) /* overdrive */
  35. #define DS1WM_INT_PD (1 << 0) /* presence detect */
  36. #define DS1WM_INT_PDR (1 << 1) /* presence detect result */
  37. #define DS1WM_INT_TBE (1 << 2) /* tx buffer empty */
  38. #define DS1WM_INT_TSRE (1 << 3) /* tx shift register empty */
  39. #define DS1WM_INT_RBF (1 << 4) /* rx buffer full */
  40. #define DS1WM_INT_RSRF (1 << 5) /* rx shift register full */
  41. #define DS1WM_INTEN_EPD (1 << 0) /* enable presence detect int */
  42. #define DS1WM_INTEN_IAS (1 << 1) /* INTR active state */
  43. #define DS1WM_INTEN_ETBE (1 << 2) /* enable tx buffer empty int */
  44. #define DS1WM_INTEN_ETMT (1 << 3) /* enable tx shift register empty int */
  45. #define DS1WM_INTEN_ERBF (1 << 4) /* enable rx buffer full int */
  46. #define DS1WM_INTEN_ERSRF (1 << 5) /* enable rx shift register full int */
  47. #define DS1WM_INTEN_DQO (1 << 6) /* enable direct bus driving ops */
  48. #define DS1WM_TIMEOUT (HZ * 5)
  49. static struct {
  50. unsigned long freq;
  51. unsigned long divisor;
  52. } freq[] = {
  53. { 4000000, 0x8 },
  54. { 5000000, 0x2 },
  55. { 6000000, 0x5 },
  56. { 7000000, 0x3 },
  57. { 8000000, 0xc },
  58. { 10000000, 0x6 },
  59. { 12000000, 0x9 },
  60. { 14000000, 0x7 },
  61. { 16000000, 0x10 },
  62. { 20000000, 0xa },
  63. { 24000000, 0xd },
  64. { 28000000, 0xb },
  65. { 32000000, 0x14 },
  66. { 40000000, 0xe },
  67. { 48000000, 0x11 },
  68. { 56000000, 0xf },
  69. { 64000000, 0x18 },
  70. { 80000000, 0x12 },
  71. { 96000000, 0x15 },
  72. { 112000000, 0x13 },
  73. { 128000000, 0x1c },
  74. };
  75. struct ds1wm_data {
  76. void __iomem *map;
  77. int bus_shift; /* # of shifts to calc register offsets */
  78. struct platform_device *pdev;
  79. struct ds1wm_platform_data *pdata;
  80. int irq;
  81. int active_high;
  82. struct clk *clk;
  83. int slave_present;
  84. void *reset_complete;
  85. void *read_complete;
  86. void *write_complete;
  87. u8 read_byte; /* last byte received */
  88. };
  89. static inline void ds1wm_write_register(struct ds1wm_data *ds1wm_data, u32 reg,
  90. u8 val)
  91. {
  92. __raw_writeb(val, ds1wm_data->map + (reg << ds1wm_data->bus_shift));
  93. }
  94. static inline u8 ds1wm_read_register(struct ds1wm_data *ds1wm_data, u32 reg)
  95. {
  96. return __raw_readb(ds1wm_data->map + (reg << ds1wm_data->bus_shift));
  97. }
  98. static irqreturn_t ds1wm_isr(int isr, void *data)
  99. {
  100. struct ds1wm_data *ds1wm_data = data;
  101. u8 intr = ds1wm_read_register(ds1wm_data, DS1WM_INT);
  102. ds1wm_data->slave_present = (intr & DS1WM_INT_PDR) ? 0 : 1;
  103. if ((intr & DS1WM_INT_PD) && ds1wm_data->reset_complete)
  104. complete(ds1wm_data->reset_complete);
  105. if ((intr & DS1WM_INT_TSRE) && ds1wm_data->write_complete)
  106. complete(ds1wm_data->write_complete);
  107. if (intr & DS1WM_INT_RBF) {
  108. ds1wm_data->read_byte = ds1wm_read_register(ds1wm_data,
  109. DS1WM_DATA);
  110. if (ds1wm_data->read_complete)
  111. complete(ds1wm_data->read_complete);
  112. }
  113. return IRQ_HANDLED;
  114. }
  115. static int ds1wm_reset(struct ds1wm_data *ds1wm_data)
  116. {
  117. unsigned long timeleft;
  118. DECLARE_COMPLETION_ONSTACK(reset_done);
  119. ds1wm_data->reset_complete = &reset_done;
  120. ds1wm_write_register(ds1wm_data, DS1WM_INT_EN, DS1WM_INTEN_EPD |
  121. (ds1wm_data->active_high ? DS1WM_INTEN_IAS : 0));
  122. ds1wm_write_register(ds1wm_data, DS1WM_CMD, DS1WM_CMD_1W_RESET);
  123. timeleft = wait_for_completion_timeout(&reset_done, DS1WM_TIMEOUT);
  124. ds1wm_data->reset_complete = NULL;
  125. if (!timeleft) {
  126. dev_dbg(&ds1wm_data->pdev->dev, "reset failed\n");
  127. return 1;
  128. }
  129. /* Wait for the end of the reset. According to the specs, the time
  130. * from when the interrupt is asserted to the end of the reset is:
  131. * tRSTH - tPDH - tPDL - tPDI
  132. * 625 us - 60 us - 240 us - 100 ns = 324.9 us
  133. *
  134. * We'll wait a bit longer just to be sure.
  135. */
  136. udelay(500);
  137. ds1wm_write_register(ds1wm_data, DS1WM_INT_EN,
  138. DS1WM_INTEN_ERBF | DS1WM_INTEN_ETMT | DS1WM_INTEN_EPD |
  139. (ds1wm_data->active_high ? DS1WM_INTEN_IAS : 0));
  140. if (!ds1wm_data->slave_present) {
  141. dev_dbg(&ds1wm_data->pdev->dev, "reset: no devices found\n");
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. static int ds1wm_write(struct ds1wm_data *ds1wm_data, u8 data)
  147. {
  148. DECLARE_COMPLETION_ONSTACK(write_done);
  149. ds1wm_data->write_complete = &write_done;
  150. ds1wm_write_register(ds1wm_data, DS1WM_DATA, data);
  151. wait_for_completion_timeout(&write_done, DS1WM_TIMEOUT);
  152. ds1wm_data->write_complete = NULL;
  153. return 0;
  154. }
  155. static int ds1wm_read(struct ds1wm_data *ds1wm_data, unsigned char write_data)
  156. {
  157. DECLARE_COMPLETION_ONSTACK(read_done);
  158. ds1wm_data->read_complete = &read_done;
  159. ds1wm_write(ds1wm_data, write_data);
  160. wait_for_completion_timeout(&read_done, DS1WM_TIMEOUT);
  161. ds1wm_data->read_complete = NULL;
  162. return ds1wm_data->read_byte;
  163. }
  164. static int ds1wm_find_divisor(int gclk)
  165. {
  166. int i;
  167. for (i = 0; i < ARRAY_SIZE(freq); i++)
  168. if (gclk <= freq[i].freq)
  169. return freq[i].divisor;
  170. return 0;
  171. }
  172. static void ds1wm_up(struct ds1wm_data *ds1wm_data)
  173. {
  174. int gclk, divisor;
  175. if (ds1wm_data->pdata->enable)
  176. ds1wm_data->pdata->enable(ds1wm_data->pdev);
  177. gclk = clk_get_rate(ds1wm_data->clk);
  178. clk_enable(ds1wm_data->clk);
  179. divisor = ds1wm_find_divisor(gclk);
  180. if (divisor == 0) {
  181. dev_err(&ds1wm_data->pdev->dev,
  182. "no suitable divisor for %dHz clock\n", gclk);
  183. return;
  184. }
  185. ds1wm_write_register(ds1wm_data, DS1WM_CLKDIV, divisor);
  186. /* Let the w1 clock stabilize. */
  187. msleep(1);
  188. ds1wm_reset(ds1wm_data);
  189. }
  190. static void ds1wm_down(struct ds1wm_data *ds1wm_data)
  191. {
  192. ds1wm_reset(ds1wm_data);
  193. /* Disable interrupts. */
  194. ds1wm_write_register(ds1wm_data, DS1WM_INT_EN,
  195. ds1wm_data->active_high ? DS1WM_INTEN_IAS : 0);
  196. if (ds1wm_data->pdata->disable)
  197. ds1wm_data->pdata->disable(ds1wm_data->pdev);
  198. clk_disable(ds1wm_data->clk);
  199. }
  200. /* --------------------------------------------------------------------- */
  201. /* w1 methods */
  202. static u8 ds1wm_read_byte(void *data)
  203. {
  204. struct ds1wm_data *ds1wm_data = data;
  205. return ds1wm_read(ds1wm_data, 0xff);
  206. }
  207. static void ds1wm_write_byte(void *data, u8 byte)
  208. {
  209. struct ds1wm_data *ds1wm_data = data;
  210. ds1wm_write(ds1wm_data, byte);
  211. }
  212. static u8 ds1wm_reset_bus(void *data)
  213. {
  214. struct ds1wm_data *ds1wm_data = data;
  215. ds1wm_reset(ds1wm_data);
  216. return 0;
  217. }
  218. static void ds1wm_search(void *data, u8 search_type,
  219. w1_slave_found_callback slave_found)
  220. {
  221. struct ds1wm_data *ds1wm_data = data;
  222. int i;
  223. unsigned long long rom_id;
  224. /* XXX We need to iterate for multiple devices per the DS1WM docs.
  225. * See http://www.maxim-ic.com/appnotes.cfm/appnote_number/120. */
  226. if (ds1wm_reset(ds1wm_data))
  227. return;
  228. ds1wm_write(ds1wm_data, search_type);
  229. ds1wm_write_register(ds1wm_data, DS1WM_CMD, DS1WM_CMD_SRA);
  230. for (rom_id = 0, i = 0; i < 16; i++) {
  231. unsigned char resp, r, d;
  232. resp = ds1wm_read(ds1wm_data, 0x00);
  233. r = ((resp & 0x02) >> 1) |
  234. ((resp & 0x08) >> 2) |
  235. ((resp & 0x20) >> 3) |
  236. ((resp & 0x80) >> 4);
  237. d = ((resp & 0x01) >> 0) |
  238. ((resp & 0x04) >> 1) |
  239. ((resp & 0x10) >> 2) |
  240. ((resp & 0x40) >> 3);
  241. rom_id |= (unsigned long long) r << (i * 4);
  242. }
  243. dev_dbg(&ds1wm_data->pdev->dev, "found 0x%08llX\n", rom_id);
  244. ds1wm_write_register(ds1wm_data, DS1WM_CMD, ~DS1WM_CMD_SRA);
  245. ds1wm_reset(ds1wm_data);
  246. slave_found(ds1wm_data, rom_id);
  247. }
  248. /* --------------------------------------------------------------------- */
  249. static struct w1_bus_master ds1wm_master = {
  250. .read_byte = ds1wm_read_byte,
  251. .write_byte = ds1wm_write_byte,
  252. .reset_bus = ds1wm_reset_bus,
  253. .search = ds1wm_search,
  254. };
  255. static int ds1wm_probe(struct platform_device *pdev)
  256. {
  257. struct ds1wm_data *ds1wm_data;
  258. struct ds1wm_platform_data *plat;
  259. struct resource *res;
  260. int ret;
  261. if (!pdev)
  262. return -ENODEV;
  263. ds1wm_data = kzalloc(sizeof (*ds1wm_data), GFP_KERNEL);
  264. if (!ds1wm_data)
  265. return -ENOMEM;
  266. platform_set_drvdata(pdev, ds1wm_data);
  267. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. if (!res) {
  269. ret = -ENXIO;
  270. goto err0;
  271. }
  272. ds1wm_data->map = ioremap(res->start, res->end - res->start + 1);
  273. if (!ds1wm_data->map) {
  274. ret = -ENOMEM;
  275. goto err0;
  276. }
  277. plat = pdev->dev.platform_data;
  278. ds1wm_data->bus_shift = plat->bus_shift;
  279. ds1wm_data->pdev = pdev;
  280. ds1wm_data->pdata = plat;
  281. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  282. if (!res) {
  283. ret = -ENXIO;
  284. goto err1;
  285. }
  286. ds1wm_data->irq = res->start;
  287. ds1wm_data->active_high = (res->flags & IORESOURCE_IRQ_HIGHEDGE) ?
  288. 1 : 0;
  289. set_irq_type(ds1wm_data->irq, ds1wm_data->active_high ?
  290. IRQ_TYPE_EDGE_RISING : IRQ_TYPE_EDGE_FALLING);
  291. ret = request_irq(ds1wm_data->irq, ds1wm_isr, IRQF_DISABLED,
  292. "ds1wm", ds1wm_data);
  293. if (ret)
  294. goto err1;
  295. ds1wm_data->clk = clk_get(&pdev->dev, "ds1wm");
  296. if (!ds1wm_data->clk) {
  297. ret = -ENOENT;
  298. goto err2;
  299. }
  300. ds1wm_up(ds1wm_data);
  301. ds1wm_master.data = (void *)ds1wm_data;
  302. ret = w1_add_master_device(&ds1wm_master);
  303. if (ret)
  304. goto err3;
  305. return 0;
  306. err3:
  307. ds1wm_down(ds1wm_data);
  308. clk_put(ds1wm_data->clk);
  309. err2:
  310. free_irq(ds1wm_data->irq, ds1wm_data);
  311. err1:
  312. iounmap(ds1wm_data->map);
  313. err0:
  314. kfree(ds1wm_data);
  315. return ret;
  316. }
  317. #ifdef CONFIG_PM
  318. static int ds1wm_suspend(struct platform_device *pdev, pm_message_t state)
  319. {
  320. struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
  321. ds1wm_down(ds1wm_data);
  322. return 0;
  323. }
  324. static int ds1wm_resume(struct platform_device *pdev)
  325. {
  326. struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
  327. ds1wm_up(ds1wm_data);
  328. return 0;
  329. }
  330. #else
  331. #define ds1wm_suspend NULL
  332. #define ds1wm_resume NULL
  333. #endif
  334. static int ds1wm_remove(struct platform_device *pdev)
  335. {
  336. struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
  337. w1_remove_master_device(&ds1wm_master);
  338. ds1wm_down(ds1wm_data);
  339. clk_put(ds1wm_data->clk);
  340. free_irq(ds1wm_data->irq, ds1wm_data);
  341. iounmap(ds1wm_data->map);
  342. kfree(ds1wm_data);
  343. return 0;
  344. }
  345. static struct platform_driver ds1wm_driver = {
  346. .driver = {
  347. .name = "ds1wm",
  348. },
  349. .probe = ds1wm_probe,
  350. .remove = ds1wm_remove,
  351. .suspend = ds1wm_suspend,
  352. .resume = ds1wm_resume
  353. };
  354. static int __init ds1wm_init(void)
  355. {
  356. printk("DS1WM w1 busmaster driver - (c) 2004 Szabolcs Gyurko\n");
  357. return platform_driver_register(&ds1wm_driver);
  358. }
  359. static void __exit ds1wm_exit(void)
  360. {
  361. platform_driver_unregister(&ds1wm_driver);
  362. }
  363. module_init(ds1wm_init);
  364. module_exit(ds1wm_exit);
  365. MODULE_LICENSE("GPL");
  366. MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
  367. "Matt Reimer <mreimer@vpop.net>");
  368. MODULE_DESCRIPTION("DS1WM w1 busmaster driver");