omap_hdq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * drivers/w1/masters/omap_hdq.c
  3. *
  4. * Copyright (C) 2007,2012 Texas Instruments, Inc.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/sched.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/of.h>
  21. #include <linux/w1.h>
  22. #define MOD_NAME "OMAP_HDQ:"
  23. #define OMAP_HDQ_REVISION 0x00
  24. #define OMAP_HDQ_TX_DATA 0x04
  25. #define OMAP_HDQ_RX_DATA 0x08
  26. #define OMAP_HDQ_CTRL_STATUS 0x0c
  27. #define OMAP_HDQ_CTRL_STATUS_SINGLE BIT(7)
  28. #define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK BIT(6)
  29. #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE BIT(5)
  30. #define OMAP_HDQ_CTRL_STATUS_GO BIT(4)
  31. #define OMAP_HDQ_CTRL_STATUS_PRESENCE BIT(3)
  32. #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION BIT(2)
  33. #define OMAP_HDQ_CTRL_STATUS_DIR BIT(1)
  34. #define OMAP_HDQ_INT_STATUS 0x10
  35. #define OMAP_HDQ_INT_STATUS_TXCOMPLETE BIT(2)
  36. #define OMAP_HDQ_INT_STATUS_RXCOMPLETE BIT(1)
  37. #define OMAP_HDQ_INT_STATUS_TIMEOUT BIT(0)
  38. #define OMAP_HDQ_FLAG_CLEAR 0
  39. #define OMAP_HDQ_FLAG_SET 1
  40. #define OMAP_HDQ_TIMEOUT (HZ/5)
  41. #define OMAP_HDQ_MAX_USER 4
  42. static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue);
  43. static int w1_id;
  44. module_param(w1_id, int, S_IRUSR);
  45. MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection in HDQ mode");
  46. struct hdq_data {
  47. struct device *dev;
  48. void __iomem *hdq_base;
  49. /* lock read/write/break operations */
  50. struct mutex hdq_mutex;
  51. /* interrupt status and a lock for it */
  52. u8 hdq_irqstatus;
  53. spinlock_t hdq_spinlock;
  54. /* mode: 0-HDQ 1-W1 */
  55. int mode;
  56. };
  57. /* HDQ register I/O routines */
  58. static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset)
  59. {
  60. return __raw_readl(hdq_data->hdq_base + offset);
  61. }
  62. static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val)
  63. {
  64. __raw_writel(val, hdq_data->hdq_base + offset);
  65. }
  66. static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset,
  67. u8 val, u8 mask)
  68. {
  69. u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask)
  70. | (val & mask);
  71. __raw_writel(new_val, hdq_data->hdq_base + offset);
  72. return new_val;
  73. }
  74. /*
  75. * Wait for one or more bits in flag change.
  76. * HDQ_FLAG_SET: wait until any bit in the flag is set.
  77. * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared.
  78. * return 0 on success and -ETIMEDOUT in the case of timeout.
  79. */
  80. static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset,
  81. u8 flag, u8 flag_set, u8 *status)
  82. {
  83. int ret = 0;
  84. unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
  85. if (flag_set == OMAP_HDQ_FLAG_CLEAR) {
  86. /* wait for the flag clear */
  87. while (((*status = hdq_reg_in(hdq_data, offset)) & flag)
  88. && time_before(jiffies, timeout)) {
  89. schedule_timeout_uninterruptible(1);
  90. }
  91. if (*status & flag)
  92. ret = -ETIMEDOUT;
  93. } else if (flag_set == OMAP_HDQ_FLAG_SET) {
  94. /* wait for the flag set */
  95. while (!((*status = hdq_reg_in(hdq_data, offset)) & flag)
  96. && time_before(jiffies, timeout)) {
  97. schedule_timeout_uninterruptible(1);
  98. }
  99. if (!(*status & flag))
  100. ret = -ETIMEDOUT;
  101. } else
  102. return -EINVAL;
  103. return ret;
  104. }
  105. /* Clear saved irqstatus after using an interrupt */
  106. static u8 hdq_reset_irqstatus(struct hdq_data *hdq_data, u8 bits)
  107. {
  108. unsigned long irqflags;
  109. u8 status;
  110. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  111. status = hdq_data->hdq_irqstatus;
  112. /* this is a read-modify-write */
  113. hdq_data->hdq_irqstatus &= ~bits;
  114. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  115. return status;
  116. }
  117. /* write out a byte and fill *status with HDQ_INT_STATUS */
  118. static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status)
  119. {
  120. int ret;
  121. u8 tmp_status;
  122. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  123. if (ret < 0) {
  124. ret = -EINTR;
  125. goto rtn;
  126. }
  127. if (hdq_data->hdq_irqstatus)
  128. dev_err(hdq_data->dev, "TX irqstatus not cleared (%02x)\n",
  129. hdq_data->hdq_irqstatus);
  130. *status = 0;
  131. hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val);
  132. /* set the GO bit */
  133. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO,
  134. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  135. /* wait for the TXCOMPLETE bit */
  136. ret = wait_event_timeout(hdq_wait_queue,
  137. (hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_TXCOMPLETE),
  138. OMAP_HDQ_TIMEOUT);
  139. *status = hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TXCOMPLETE);
  140. if (ret == 0) {
  141. dev_dbg(hdq_data->dev, "TX wait elapsed\n");
  142. ret = -ETIMEDOUT;
  143. goto out;
  144. }
  145. /* check irqstatus */
  146. if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) {
  147. dev_dbg(hdq_data->dev, "timeout waiting for"
  148. " TXCOMPLETE/RXCOMPLETE, %x\n", *status);
  149. ret = -ETIMEDOUT;
  150. goto out;
  151. }
  152. /* wait for the GO bit return to zero */
  153. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  154. OMAP_HDQ_CTRL_STATUS_GO,
  155. OMAP_HDQ_FLAG_CLEAR, &tmp_status);
  156. if (ret) {
  157. dev_dbg(hdq_data->dev, "timeout waiting GO bit"
  158. " return to zero, %x\n", tmp_status);
  159. }
  160. out:
  161. mutex_unlock(&hdq_data->hdq_mutex);
  162. rtn:
  163. return ret;
  164. }
  165. /* HDQ Interrupt service routine */
  166. static irqreturn_t hdq_isr(int irq, void *_hdq)
  167. {
  168. struct hdq_data *hdq_data = _hdq;
  169. unsigned long irqflags;
  170. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  171. hdq_data->hdq_irqstatus |= hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  172. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  173. dev_dbg(hdq_data->dev, "hdq_isr: %x\n", hdq_data->hdq_irqstatus);
  174. if (hdq_data->hdq_irqstatus &
  175. (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE
  176. | OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  177. /* wake up sleeping process */
  178. wake_up(&hdq_wait_queue);
  179. }
  180. return IRQ_HANDLED;
  181. }
  182. /* W1 search callback function in HDQ mode */
  183. static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev,
  184. u8 search_type, w1_slave_found_callback slave_found)
  185. {
  186. u64 module_id, rn_le, cs, id;
  187. if (w1_id)
  188. module_id = w1_id;
  189. else
  190. module_id = 0x1;
  191. rn_le = cpu_to_le64(module_id);
  192. /*
  193. * HDQ might not obey truly the 1-wire spec.
  194. * So calculate CRC based on module parameter.
  195. */
  196. cs = w1_calc_crc8((u8 *)&rn_le, 7);
  197. id = (cs << 56) | module_id;
  198. slave_found(master_dev, id);
  199. }
  200. /* Issue break pulse to the device */
  201. static int omap_hdq_break(struct hdq_data *hdq_data)
  202. {
  203. int ret = 0;
  204. u8 tmp_status;
  205. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  206. if (ret < 0) {
  207. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  208. ret = -EINTR;
  209. goto rtn;
  210. }
  211. if (hdq_data->hdq_irqstatus)
  212. dev_err(hdq_data->dev, "break irqstatus not cleared (%02x)\n",
  213. hdq_data->hdq_irqstatus);
  214. /* set the INIT and GO bit */
  215. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  216. OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO,
  217. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  218. OMAP_HDQ_CTRL_STATUS_GO);
  219. /* wait for the TIMEOUT bit */
  220. ret = wait_event_timeout(hdq_wait_queue,
  221. (hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_TIMEOUT),
  222. OMAP_HDQ_TIMEOUT);
  223. tmp_status = hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TIMEOUT);
  224. if (ret == 0) {
  225. dev_dbg(hdq_data->dev, "break wait elapsed\n");
  226. ret = -EINTR;
  227. goto out;
  228. }
  229. /* check irqstatus */
  230. if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  231. dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x\n",
  232. tmp_status);
  233. ret = -ETIMEDOUT;
  234. goto out;
  235. }
  236. /*
  237. * check for the presence detect bit to get
  238. * set to show that the slave is responding
  239. */
  240. if (!(hdq_reg_in(hdq_data, OMAP_HDQ_CTRL_STATUS) &
  241. OMAP_HDQ_CTRL_STATUS_PRESENCE)) {
  242. dev_dbg(hdq_data->dev, "Presence bit not set\n");
  243. ret = -ETIMEDOUT;
  244. goto out;
  245. }
  246. /*
  247. * wait for both INIT and GO bits rerurn to zero.
  248. * zero wait time expected for interrupt mode.
  249. */
  250. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  251. OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  252. OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR,
  253. &tmp_status);
  254. if (ret)
  255. dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits"
  256. " return to zero, %x\n", tmp_status);
  257. out:
  258. mutex_unlock(&hdq_data->hdq_mutex);
  259. rtn:
  260. return ret;
  261. }
  262. static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val)
  263. {
  264. int ret = 0;
  265. u8 status;
  266. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  267. if (ret < 0) {
  268. ret = -EINTR;
  269. goto rtn;
  270. }
  271. if (pm_runtime_suspended(hdq_data->dev)) {
  272. ret = -EINVAL;
  273. goto out;
  274. }
  275. if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  276. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  277. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO,
  278. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  279. /*
  280. * The RX comes immediately after TX.
  281. */
  282. wait_event_timeout(hdq_wait_queue,
  283. (hdq_data->hdq_irqstatus
  284. & (OMAP_HDQ_INT_STATUS_RXCOMPLETE |
  285. OMAP_HDQ_INT_STATUS_TIMEOUT)),
  286. OMAP_HDQ_TIMEOUT);
  287. status = hdq_reset_irqstatus(hdq_data,
  288. OMAP_HDQ_INT_STATUS_RXCOMPLETE |
  289. OMAP_HDQ_INT_STATUS_TIMEOUT);
  290. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0,
  291. OMAP_HDQ_CTRL_STATUS_DIR);
  292. /* check irqstatus */
  293. if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  294. dev_dbg(hdq_data->dev, "timeout waiting for"
  295. " RXCOMPLETE, %x", status);
  296. ret = -ETIMEDOUT;
  297. goto out;
  298. }
  299. } else { /* interrupt had occurred before hdq_read_byte was called */
  300. hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
  301. }
  302. /* the data is ready. Read it in! */
  303. *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA);
  304. out:
  305. mutex_unlock(&hdq_data->hdq_mutex);
  306. rtn:
  307. return ret;
  308. }
  309. /*
  310. * W1 triplet callback function - used for searching ROM addresses.
  311. * Registered only when controller is in 1-wire mode.
  312. */
  313. static u8 omap_w1_triplet(void *_hdq, u8 bdir)
  314. {
  315. u8 id_bit, comp_bit;
  316. int err;
  317. u8 ret = 0x3; /* no slaves responded */
  318. struct hdq_data *hdq_data = _hdq;
  319. u8 ctrl = OMAP_HDQ_CTRL_STATUS_SINGLE | OMAP_HDQ_CTRL_STATUS_GO |
  320. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK;
  321. u8 mask = ctrl | OMAP_HDQ_CTRL_STATUS_DIR;
  322. err = pm_runtime_get_sync(hdq_data->dev);
  323. if (err < 0) {
  324. pm_runtime_put_noidle(hdq_data->dev);
  325. return err;
  326. }
  327. err = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  328. if (err < 0) {
  329. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  330. goto rtn;
  331. }
  332. /* read id_bit */
  333. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
  334. ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
  335. err = wait_event_timeout(hdq_wait_queue,
  336. (hdq_data->hdq_irqstatus
  337. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  338. OMAP_HDQ_TIMEOUT);
  339. /* Must clear irqstatus for another RXCOMPLETE interrupt */
  340. hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
  341. if (err == 0) {
  342. dev_dbg(hdq_data->dev, "RX wait elapsed\n");
  343. goto out;
  344. }
  345. id_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
  346. /* read comp_bit */
  347. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
  348. ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
  349. err = wait_event_timeout(hdq_wait_queue,
  350. (hdq_data->hdq_irqstatus
  351. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  352. OMAP_HDQ_TIMEOUT);
  353. /* Must clear irqstatus for another RXCOMPLETE interrupt */
  354. hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_RXCOMPLETE);
  355. if (err == 0) {
  356. dev_dbg(hdq_data->dev, "RX wait elapsed\n");
  357. goto out;
  358. }
  359. comp_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
  360. if (id_bit && comp_bit) {
  361. ret = 0x03; /* no slaves responded */
  362. goto out;
  363. }
  364. if (!id_bit && !comp_bit) {
  365. /* Both bits are valid, take the direction given */
  366. ret = bdir ? 0x04 : 0;
  367. } else {
  368. /* Only one bit is valid, take that direction */
  369. bdir = id_bit;
  370. ret = id_bit ? 0x05 : 0x02;
  371. }
  372. /* write bdir bit */
  373. hdq_reg_out(_hdq, OMAP_HDQ_TX_DATA, bdir);
  374. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, ctrl, mask);
  375. err = wait_event_timeout(hdq_wait_queue,
  376. (hdq_data->hdq_irqstatus
  377. & OMAP_HDQ_INT_STATUS_TXCOMPLETE),
  378. OMAP_HDQ_TIMEOUT);
  379. /* Must clear irqstatus for another TXCOMPLETE interrupt */
  380. hdq_reset_irqstatus(hdq_data, OMAP_HDQ_INT_STATUS_TXCOMPLETE);
  381. if (err == 0) {
  382. dev_dbg(hdq_data->dev, "TX wait elapsed\n");
  383. goto out;
  384. }
  385. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, 0,
  386. OMAP_HDQ_CTRL_STATUS_SINGLE);
  387. out:
  388. mutex_unlock(&hdq_data->hdq_mutex);
  389. rtn:
  390. pm_runtime_mark_last_busy(hdq_data->dev);
  391. pm_runtime_put_autosuspend(hdq_data->dev);
  392. return ret;
  393. }
  394. /* reset callback */
  395. static u8 omap_w1_reset_bus(void *_hdq)
  396. {
  397. struct hdq_data *hdq_data = _hdq;
  398. int err;
  399. err = pm_runtime_get_sync(hdq_data->dev);
  400. if (err < 0) {
  401. pm_runtime_put_noidle(hdq_data->dev);
  402. return err;
  403. }
  404. omap_hdq_break(hdq_data);
  405. pm_runtime_mark_last_busy(hdq_data->dev);
  406. pm_runtime_put_autosuspend(hdq_data->dev);
  407. return 0;
  408. }
  409. /* Read a byte of data from the device */
  410. static u8 omap_w1_read_byte(void *_hdq)
  411. {
  412. struct hdq_data *hdq_data = _hdq;
  413. u8 val = 0;
  414. int ret;
  415. ret = pm_runtime_get_sync(hdq_data->dev);
  416. if (ret < 0) {
  417. pm_runtime_put_noidle(hdq_data->dev);
  418. return -1;
  419. }
  420. ret = hdq_read_byte(hdq_data, &val);
  421. if (ret)
  422. val = -1;
  423. pm_runtime_mark_last_busy(hdq_data->dev);
  424. pm_runtime_put_autosuspend(hdq_data->dev);
  425. return val;
  426. }
  427. /* Write a byte of data to the device */
  428. static void omap_w1_write_byte(void *_hdq, u8 byte)
  429. {
  430. struct hdq_data *hdq_data = _hdq;
  431. int ret;
  432. u8 status;
  433. ret = pm_runtime_get_sync(hdq_data->dev);
  434. if (ret < 0) {
  435. pm_runtime_put_noidle(hdq_data->dev);
  436. return;
  437. }
  438. /*
  439. * We need to reset the slave before
  440. * issuing the SKIP ROM command, else
  441. * the slave will not work.
  442. */
  443. if (byte == W1_SKIP_ROM)
  444. omap_hdq_break(hdq_data);
  445. ret = hdq_write_byte(hdq_data, byte, &status);
  446. if (ret < 0) {
  447. dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status);
  448. goto out_err;
  449. }
  450. out_err:
  451. pm_runtime_mark_last_busy(hdq_data->dev);
  452. pm_runtime_put_autosuspend(hdq_data->dev);
  453. }
  454. static struct w1_bus_master omap_w1_master = {
  455. .read_byte = omap_w1_read_byte,
  456. .write_byte = omap_w1_write_byte,
  457. .reset_bus = omap_w1_reset_bus,
  458. };
  459. static int __maybe_unused omap_hdq_runtime_suspend(struct device *dev)
  460. {
  461. struct hdq_data *hdq_data = dev_get_drvdata(dev);
  462. hdq_reg_out(hdq_data, 0, hdq_data->mode);
  463. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  464. return 0;
  465. }
  466. static int __maybe_unused omap_hdq_runtime_resume(struct device *dev)
  467. {
  468. struct hdq_data *hdq_data = dev_get_drvdata(dev);
  469. /* select HDQ/1W mode & enable clocks */
  470. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  471. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  472. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK |
  473. hdq_data->mode);
  474. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  475. return 0;
  476. }
  477. static const struct dev_pm_ops omap_hdq_pm_ops = {
  478. SET_RUNTIME_PM_OPS(omap_hdq_runtime_suspend,
  479. omap_hdq_runtime_resume, NULL)
  480. };
  481. static int omap_hdq_probe(struct platform_device *pdev)
  482. {
  483. struct device *dev = &pdev->dev;
  484. struct hdq_data *hdq_data;
  485. int ret, irq;
  486. u8 rev;
  487. const char *mode;
  488. hdq_data = devm_kzalloc(dev, sizeof(*hdq_data), GFP_KERNEL);
  489. if (!hdq_data) {
  490. dev_dbg(&pdev->dev, "unable to allocate memory\n");
  491. return -ENOMEM;
  492. }
  493. hdq_data->dev = dev;
  494. platform_set_drvdata(pdev, hdq_data);
  495. hdq_data->hdq_base = devm_platform_ioremap_resource(pdev, 0);
  496. if (IS_ERR(hdq_data->hdq_base))
  497. return PTR_ERR(hdq_data->hdq_base);
  498. mutex_init(&hdq_data->hdq_mutex);
  499. ret = of_property_read_string(pdev->dev.of_node, "ti,mode", &mode);
  500. if (ret < 0 || !strcmp(mode, "hdq")) {
  501. hdq_data->mode = 0;
  502. omap_w1_master.search = omap_w1_search_bus;
  503. } else {
  504. hdq_data->mode = 1;
  505. omap_w1_master.triplet = omap_w1_triplet;
  506. }
  507. pm_runtime_enable(&pdev->dev);
  508. pm_runtime_use_autosuspend(&pdev->dev);
  509. pm_runtime_set_autosuspend_delay(&pdev->dev, 300);
  510. ret = pm_runtime_get_sync(&pdev->dev);
  511. if (ret < 0) {
  512. pm_runtime_put_noidle(&pdev->dev);
  513. dev_dbg(&pdev->dev, "pm_runtime_get_sync failed\n");
  514. goto err_w1;
  515. }
  516. rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION);
  517. dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n",
  518. (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt");
  519. spin_lock_init(&hdq_data->hdq_spinlock);
  520. irq = platform_get_irq(pdev, 0);
  521. if (irq < 0) {
  522. dev_dbg(&pdev->dev, "Failed to get IRQ: %d\n", irq);
  523. ret = irq;
  524. goto err_irq;
  525. }
  526. ret = devm_request_irq(dev, irq, hdq_isr, 0, "omap_hdq", hdq_data);
  527. if (ret < 0) {
  528. dev_dbg(&pdev->dev, "could not request irq\n");
  529. goto err_irq;
  530. }
  531. omap_hdq_break(hdq_data);
  532. pm_runtime_mark_last_busy(&pdev->dev);
  533. pm_runtime_put_autosuspend(&pdev->dev);
  534. omap_w1_master.data = hdq_data;
  535. ret = w1_add_master_device(&omap_w1_master);
  536. if (ret) {
  537. dev_dbg(&pdev->dev, "Failure in registering w1 master\n");
  538. goto err_w1;
  539. }
  540. return 0;
  541. err_irq:
  542. pm_runtime_put_sync(&pdev->dev);
  543. err_w1:
  544. pm_runtime_dont_use_autosuspend(&pdev->dev);
  545. pm_runtime_disable(&pdev->dev);
  546. return ret;
  547. }
  548. static int omap_hdq_remove(struct platform_device *pdev)
  549. {
  550. int active;
  551. active = pm_runtime_get_sync(&pdev->dev);
  552. if (active < 0)
  553. pm_runtime_put_noidle(&pdev->dev);
  554. w1_remove_master_device(&omap_w1_master);
  555. pm_runtime_dont_use_autosuspend(&pdev->dev);
  556. if (active >= 0)
  557. pm_runtime_put_sync(&pdev->dev);
  558. pm_runtime_disable(&pdev->dev);
  559. return 0;
  560. }
  561. static const struct of_device_id omap_hdq_dt_ids[] = {
  562. { .compatible = "ti,omap3-1w" },
  563. { .compatible = "ti,am4372-hdq" },
  564. {}
  565. };
  566. MODULE_DEVICE_TABLE(of, omap_hdq_dt_ids);
  567. static struct platform_driver omap_hdq_driver = {
  568. .probe = omap_hdq_probe,
  569. .remove = omap_hdq_remove,
  570. .driver = {
  571. .name = "omap_hdq",
  572. .of_match_table = omap_hdq_dt_ids,
  573. .pm = &omap_hdq_pm_ops,
  574. },
  575. };
  576. module_platform_driver(omap_hdq_driver);
  577. MODULE_AUTHOR("Texas Instruments");
  578. MODULE_DESCRIPTION("HDQ-1W driver Library");
  579. MODULE_LICENSE("GPL");