omap_hdq.c 17 KB

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