ds1wm.c 12 KB

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