omap_hdq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 "../w1.h"
  22. #include "../w1_int.h"
  23. #define MOD_NAME "OMAP_HDQ:"
  24. #define OMAP_HDQ_REVISION 0x00
  25. #define OMAP_HDQ_TX_DATA 0x04
  26. #define OMAP_HDQ_RX_DATA 0x08
  27. #define OMAP_HDQ_CTRL_STATUS 0x0c
  28. #define OMAP_HDQ_CTRL_STATUS_SINGLE BIT(7)
  29. #define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK BIT(6)
  30. #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE BIT(5)
  31. #define OMAP_HDQ_CTRL_STATUS_GO BIT(4)
  32. #define OMAP_HDQ_CTRL_STATUS_PRESENCE BIT(3)
  33. #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION BIT(2)
  34. #define OMAP_HDQ_CTRL_STATUS_DIR BIT(1)
  35. #define OMAP_HDQ_INT_STATUS 0x10
  36. #define OMAP_HDQ_INT_STATUS_TXCOMPLETE BIT(2)
  37. #define OMAP_HDQ_INT_STATUS_RXCOMPLETE BIT(1)
  38. #define OMAP_HDQ_INT_STATUS_TIMEOUT BIT(0)
  39. #define OMAP_HDQ_SYSCONFIG 0x14
  40. #define OMAP_HDQ_SYSCONFIG_SOFTRESET BIT(1)
  41. #define OMAP_HDQ_SYSCONFIG_AUTOIDLE BIT(0)
  42. #define OMAP_HDQ_SYSCONFIG_NOIDLE 0x0
  43. #define OMAP_HDQ_SYSSTATUS 0x18
  44. #define OMAP_HDQ_SYSSTATUS_RESETDONE BIT(0)
  45. #define OMAP_HDQ_FLAG_CLEAR 0
  46. #define OMAP_HDQ_FLAG_SET 1
  47. #define OMAP_HDQ_TIMEOUT (HZ/5)
  48. #define OMAP_HDQ_MAX_USER 4
  49. static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue);
  50. static int w1_id;
  51. struct hdq_data {
  52. struct device *dev;
  53. void __iomem *hdq_base;
  54. /* lock status update */
  55. struct mutex hdq_mutex;
  56. int hdq_usecount;
  57. u8 hdq_irqstatus;
  58. /* device lock */
  59. spinlock_t hdq_spinlock;
  60. /*
  61. * Used to control the call to omap_hdq_get and omap_hdq_put.
  62. * HDQ Protocol: Write the CMD|REG_address first, followed by
  63. * the data wrire or read.
  64. */
  65. int init_trans;
  66. int rrw;
  67. /* mode: 0-HDQ 1-W1 */
  68. int mode;
  69. };
  70. static int omap_hdq_probe(struct platform_device *pdev);
  71. static int omap_hdq_remove(struct platform_device *pdev);
  72. static const struct of_device_id omap_hdq_dt_ids[] = {
  73. { .compatible = "ti,omap3-1w" },
  74. { .compatible = "ti,am4372-hdq" },
  75. {}
  76. };
  77. MODULE_DEVICE_TABLE(of, omap_hdq_dt_ids);
  78. static struct platform_driver omap_hdq_driver = {
  79. .probe = omap_hdq_probe,
  80. .remove = omap_hdq_remove,
  81. .driver = {
  82. .name = "omap_hdq",
  83. .of_match_table = omap_hdq_dt_ids,
  84. },
  85. };
  86. static u8 omap_w1_read_byte(void *_hdq);
  87. static void omap_w1_write_byte(void *_hdq, u8 byte);
  88. static u8 omap_w1_reset_bus(void *_hdq);
  89. static struct w1_bus_master omap_w1_master = {
  90. .read_byte = omap_w1_read_byte,
  91. .write_byte = omap_w1_write_byte,
  92. .reset_bus = omap_w1_reset_bus,
  93. };
  94. /* HDQ register I/O routines */
  95. static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset)
  96. {
  97. return __raw_readl(hdq_data->hdq_base + offset);
  98. }
  99. static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val)
  100. {
  101. __raw_writel(val, hdq_data->hdq_base + offset);
  102. }
  103. static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset,
  104. u8 val, u8 mask)
  105. {
  106. u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask)
  107. | (val & mask);
  108. __raw_writel(new_val, hdq_data->hdq_base + offset);
  109. return new_val;
  110. }
  111. static void hdq_disable_interrupt(struct hdq_data *hdq_data, u32 offset,
  112. u32 mask)
  113. {
  114. u32 ie;
  115. ie = readl(hdq_data->hdq_base + offset);
  116. writel(ie & mask, hdq_data->hdq_base + offset);
  117. }
  118. /*
  119. * Wait for one or more bits in flag change.
  120. * HDQ_FLAG_SET: wait until any bit in the flag is set.
  121. * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared.
  122. * return 0 on success and -ETIMEDOUT in the case of timeout.
  123. */
  124. static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset,
  125. u8 flag, u8 flag_set, u8 *status)
  126. {
  127. int ret = 0;
  128. unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
  129. if (flag_set == OMAP_HDQ_FLAG_CLEAR) {
  130. /* wait for the flag clear */
  131. while (((*status = hdq_reg_in(hdq_data, offset)) & flag)
  132. && time_before(jiffies, timeout)) {
  133. schedule_timeout_uninterruptible(1);
  134. }
  135. if (*status & flag)
  136. ret = -ETIMEDOUT;
  137. } else if (flag_set == OMAP_HDQ_FLAG_SET) {
  138. /* wait for the flag set */
  139. while (!((*status = hdq_reg_in(hdq_data, offset)) & flag)
  140. && time_before(jiffies, timeout)) {
  141. schedule_timeout_uninterruptible(1);
  142. }
  143. if (!(*status & flag))
  144. ret = -ETIMEDOUT;
  145. } else
  146. return -EINVAL;
  147. return ret;
  148. }
  149. /* write out a byte and fill *status with HDQ_INT_STATUS */
  150. static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status)
  151. {
  152. int ret;
  153. u8 tmp_status;
  154. unsigned long irqflags;
  155. *status = 0;
  156. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  157. /* clear interrupt flags via a dummy read */
  158. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  159. /* ISR loads it with new INT_STATUS */
  160. hdq_data->hdq_irqstatus = 0;
  161. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  162. hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val);
  163. /* set the GO bit */
  164. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO,
  165. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  166. /* wait for the TXCOMPLETE bit */
  167. ret = wait_event_timeout(hdq_wait_queue,
  168. hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
  169. if (ret == 0) {
  170. dev_dbg(hdq_data->dev, "TX wait elapsed\n");
  171. ret = -ETIMEDOUT;
  172. goto out;
  173. }
  174. *status = hdq_data->hdq_irqstatus;
  175. /* check irqstatus */
  176. if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) {
  177. dev_dbg(hdq_data->dev, "timeout waiting for"
  178. " TXCOMPLETE/RXCOMPLETE, %x", *status);
  179. ret = -ETIMEDOUT;
  180. goto out;
  181. }
  182. /* wait for the GO bit return to zero */
  183. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  184. OMAP_HDQ_CTRL_STATUS_GO,
  185. OMAP_HDQ_FLAG_CLEAR, &tmp_status);
  186. if (ret) {
  187. dev_dbg(hdq_data->dev, "timeout waiting GO bit"
  188. " return to zero, %x", tmp_status);
  189. }
  190. out:
  191. return ret;
  192. }
  193. /* HDQ Interrupt service routine */
  194. static irqreturn_t hdq_isr(int irq, void *_hdq)
  195. {
  196. struct hdq_data *hdq_data = _hdq;
  197. unsigned long irqflags;
  198. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  199. hdq_data->hdq_irqstatus = hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  200. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  201. dev_dbg(hdq_data->dev, "hdq_isr: %x", hdq_data->hdq_irqstatus);
  202. if (hdq_data->hdq_irqstatus &
  203. (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE
  204. | OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  205. /* wake up sleeping process */
  206. wake_up(&hdq_wait_queue);
  207. }
  208. return IRQ_HANDLED;
  209. }
  210. /* W1 search callback function in HDQ mode */
  211. static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev,
  212. u8 search_type, w1_slave_found_callback slave_found)
  213. {
  214. u64 module_id, rn_le, cs, id;
  215. if (w1_id)
  216. module_id = w1_id;
  217. else
  218. module_id = 0x1;
  219. rn_le = cpu_to_le64(module_id);
  220. /*
  221. * HDQ might not obey truly the 1-wire spec.
  222. * So calculate CRC based on module parameter.
  223. */
  224. cs = w1_calc_crc8((u8 *)&rn_le, 7);
  225. id = (cs << 56) | module_id;
  226. slave_found(master_dev, id);
  227. }
  228. static int _omap_hdq_reset(struct hdq_data *hdq_data)
  229. {
  230. int ret;
  231. u8 tmp_status;
  232. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  233. OMAP_HDQ_SYSCONFIG_SOFTRESET);
  234. /*
  235. * Select HDQ/1W mode & enable clocks.
  236. * It is observed that INT flags can't be cleared via a read and GO/INIT
  237. * won't return to zero if interrupt is disabled. So we always enable
  238. * interrupt.
  239. */
  240. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  241. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  242. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
  243. /* wait for reset to complete */
  244. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_SYSSTATUS,
  245. OMAP_HDQ_SYSSTATUS_RESETDONE, OMAP_HDQ_FLAG_SET, &tmp_status);
  246. if (ret)
  247. dev_dbg(hdq_data->dev, "timeout waiting HDQ reset, %x",
  248. tmp_status);
  249. else {
  250. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  251. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  252. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK |
  253. hdq_data->mode);
  254. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  255. OMAP_HDQ_SYSCONFIG_AUTOIDLE);
  256. }
  257. return ret;
  258. }
  259. /* Issue break pulse to the device */
  260. static int omap_hdq_break(struct hdq_data *hdq_data)
  261. {
  262. int ret = 0;
  263. u8 tmp_status;
  264. unsigned long irqflags;
  265. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  266. if (ret < 0) {
  267. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  268. ret = -EINTR;
  269. goto rtn;
  270. }
  271. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  272. /* clear interrupt flags via a dummy read */
  273. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  274. /* ISR loads it with new INT_STATUS */
  275. hdq_data->hdq_irqstatus = 0;
  276. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  277. /* set the INIT and GO bit */
  278. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  279. OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO,
  280. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  281. OMAP_HDQ_CTRL_STATUS_GO);
  282. /* wait for the TIMEOUT bit */
  283. ret = wait_event_timeout(hdq_wait_queue,
  284. hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
  285. if (ret == 0) {
  286. dev_dbg(hdq_data->dev, "break wait elapsed\n");
  287. ret = -EINTR;
  288. goto out;
  289. }
  290. tmp_status = hdq_data->hdq_irqstatus;
  291. /* check irqstatus */
  292. if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  293. dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x",
  294. tmp_status);
  295. ret = -ETIMEDOUT;
  296. goto out;
  297. }
  298. /*
  299. * check for the presence detect bit to get
  300. * set to show that the slave is responding
  301. */
  302. if (!(hdq_reg_in(hdq_data, OMAP_HDQ_CTRL_STATUS) &
  303. OMAP_HDQ_CTRL_STATUS_PRESENCE)) {
  304. dev_dbg(hdq_data->dev, "Presence bit not set\n");
  305. ret = -ETIMEDOUT;
  306. goto out;
  307. }
  308. /*
  309. * wait for both INIT and GO bits rerurn to zero.
  310. * zero wait time expected for interrupt mode.
  311. */
  312. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  313. OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  314. OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR,
  315. &tmp_status);
  316. if (ret)
  317. dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits"
  318. " return to zero, %x", tmp_status);
  319. out:
  320. mutex_unlock(&hdq_data->hdq_mutex);
  321. rtn:
  322. return ret;
  323. }
  324. static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val)
  325. {
  326. int ret = 0;
  327. u8 status;
  328. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  329. if (ret < 0) {
  330. ret = -EINTR;
  331. goto rtn;
  332. }
  333. if (!hdq_data->hdq_usecount) {
  334. ret = -EINVAL;
  335. goto out;
  336. }
  337. if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  338. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  339. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO,
  340. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  341. /*
  342. * The RX comes immediately after TX.
  343. */
  344. wait_event_timeout(hdq_wait_queue,
  345. (hdq_data->hdq_irqstatus
  346. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  347. OMAP_HDQ_TIMEOUT);
  348. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0,
  349. OMAP_HDQ_CTRL_STATUS_DIR);
  350. status = hdq_data->hdq_irqstatus;
  351. /* check irqstatus */
  352. if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  353. dev_dbg(hdq_data->dev, "timeout waiting for"
  354. " RXCOMPLETE, %x", status);
  355. ret = -ETIMEDOUT;
  356. goto out;
  357. }
  358. }
  359. /* the data is ready. Read it in! */
  360. *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA);
  361. out:
  362. mutex_unlock(&hdq_data->hdq_mutex);
  363. rtn:
  364. return ret;
  365. }
  366. /* Enable clocks and set the controller to HDQ/1W mode */
  367. static int omap_hdq_get(struct hdq_data *hdq_data)
  368. {
  369. int ret = 0;
  370. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  371. if (ret < 0) {
  372. ret = -EINTR;
  373. goto rtn;
  374. }
  375. if (OMAP_HDQ_MAX_USER == hdq_data->hdq_usecount) {
  376. dev_dbg(hdq_data->dev, "attempt to exceed the max use count");
  377. ret = -EINVAL;
  378. goto out;
  379. } else {
  380. hdq_data->hdq_usecount++;
  381. try_module_get(THIS_MODULE);
  382. if (1 == hdq_data->hdq_usecount) {
  383. pm_runtime_get_sync(hdq_data->dev);
  384. /* make sure HDQ/1W is out of reset */
  385. if (!(hdq_reg_in(hdq_data, OMAP_HDQ_SYSSTATUS) &
  386. OMAP_HDQ_SYSSTATUS_RESETDONE)) {
  387. ret = _omap_hdq_reset(hdq_data);
  388. if (ret)
  389. /* back up the count */
  390. hdq_data->hdq_usecount--;
  391. } else {
  392. /* select HDQ/1W mode & enable clocks */
  393. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  394. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  395. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK |
  396. hdq_data->mode);
  397. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  398. OMAP_HDQ_SYSCONFIG_NOIDLE);
  399. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  400. }
  401. }
  402. }
  403. out:
  404. mutex_unlock(&hdq_data->hdq_mutex);
  405. rtn:
  406. return ret;
  407. }
  408. /* Disable clocks to the module */
  409. static int omap_hdq_put(struct hdq_data *hdq_data)
  410. {
  411. int ret = 0;
  412. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  413. if (ret < 0)
  414. return -EINTR;
  415. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  416. OMAP_HDQ_SYSCONFIG_AUTOIDLE);
  417. if (0 == hdq_data->hdq_usecount) {
  418. dev_dbg(hdq_data->dev, "attempt to decrement use count"
  419. " when it is zero");
  420. ret = -EINVAL;
  421. } else {
  422. hdq_data->hdq_usecount--;
  423. module_put(THIS_MODULE);
  424. if (0 == hdq_data->hdq_usecount)
  425. pm_runtime_put_sync(hdq_data->dev);
  426. }
  427. mutex_unlock(&hdq_data->hdq_mutex);
  428. return ret;
  429. }
  430. /*
  431. * W1 triplet callback function - used for searching ROM addresses.
  432. * Registered only when controller is in 1-wire mode.
  433. */
  434. static u8 omap_w1_triplet(void *_hdq, u8 bdir)
  435. {
  436. u8 id_bit, comp_bit;
  437. int err;
  438. u8 ret = 0x3; /* no slaves responded */
  439. struct hdq_data *hdq_data = _hdq;
  440. u8 ctrl = OMAP_HDQ_CTRL_STATUS_SINGLE | OMAP_HDQ_CTRL_STATUS_GO |
  441. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK;
  442. u8 mask = ctrl | OMAP_HDQ_CTRL_STATUS_DIR;
  443. omap_hdq_get(_hdq);
  444. err = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  445. if (err < 0) {
  446. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  447. goto rtn;
  448. }
  449. hdq_data->hdq_irqstatus = 0;
  450. /* read id_bit */
  451. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
  452. ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
  453. err = wait_event_timeout(hdq_wait_queue,
  454. (hdq_data->hdq_irqstatus
  455. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  456. OMAP_HDQ_TIMEOUT);
  457. if (err == 0) {
  458. dev_dbg(hdq_data->dev, "RX wait elapsed\n");
  459. goto out;
  460. }
  461. id_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
  462. hdq_data->hdq_irqstatus = 0;
  463. /* read comp_bit */
  464. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS,
  465. ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask);
  466. err = wait_event_timeout(hdq_wait_queue,
  467. (hdq_data->hdq_irqstatus
  468. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  469. OMAP_HDQ_TIMEOUT);
  470. if (err == 0) {
  471. dev_dbg(hdq_data->dev, "RX wait elapsed\n");
  472. goto out;
  473. }
  474. comp_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01);
  475. if (id_bit && comp_bit) {
  476. ret = 0x03; /* no slaves responded */
  477. goto out;
  478. }
  479. if (!id_bit && !comp_bit) {
  480. /* Both bits are valid, take the direction given */
  481. ret = bdir ? 0x04 : 0;
  482. } else {
  483. /* Only one bit is valid, take that direction */
  484. bdir = id_bit;
  485. ret = id_bit ? 0x05 : 0x02;
  486. }
  487. /* write bdir bit */
  488. hdq_reg_out(_hdq, OMAP_HDQ_TX_DATA, bdir);
  489. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, ctrl, mask);
  490. err = wait_event_timeout(hdq_wait_queue,
  491. (hdq_data->hdq_irqstatus
  492. & OMAP_HDQ_INT_STATUS_TXCOMPLETE),
  493. OMAP_HDQ_TIMEOUT);
  494. if (err == 0) {
  495. dev_dbg(hdq_data->dev, "TX wait elapsed\n");
  496. goto out;
  497. }
  498. hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, 0,
  499. OMAP_HDQ_CTRL_STATUS_SINGLE);
  500. out:
  501. mutex_unlock(&hdq_data->hdq_mutex);
  502. rtn:
  503. omap_hdq_put(_hdq);
  504. return ret;
  505. }
  506. /* reset callback */
  507. static u8 omap_w1_reset_bus(void *_hdq)
  508. {
  509. omap_hdq_get(_hdq);
  510. omap_hdq_break(_hdq);
  511. omap_hdq_put(_hdq);
  512. return 0;
  513. }
  514. /* Read a byte of data from the device */
  515. static u8 omap_w1_read_byte(void *_hdq)
  516. {
  517. struct hdq_data *hdq_data = _hdq;
  518. u8 val = 0;
  519. int ret;
  520. /* First write to initialize the transfer */
  521. if (hdq_data->init_trans == 0)
  522. omap_hdq_get(hdq_data);
  523. ret = hdq_read_byte(hdq_data, &val);
  524. if (ret) {
  525. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  526. if (ret < 0) {
  527. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  528. return -EINTR;
  529. }
  530. hdq_data->init_trans = 0;
  531. mutex_unlock(&hdq_data->hdq_mutex);
  532. omap_hdq_put(hdq_data);
  533. return -1;
  534. }
  535. hdq_disable_interrupt(hdq_data, OMAP_HDQ_CTRL_STATUS,
  536. ~OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
  537. hdq_data->hdq_usecount = 0;
  538. /* Write followed by a read, release the module */
  539. if (hdq_data->init_trans) {
  540. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  541. if (ret < 0) {
  542. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  543. return -EINTR;
  544. }
  545. hdq_data->init_trans = 0;
  546. mutex_unlock(&hdq_data->hdq_mutex);
  547. omap_hdq_put(hdq_data);
  548. }
  549. return val;
  550. }
  551. /* Write a byte of data to the device */
  552. static void omap_w1_write_byte(void *_hdq, u8 byte)
  553. {
  554. struct hdq_data *hdq_data = _hdq;
  555. int ret;
  556. u8 status;
  557. /* First write to initialize the transfer */
  558. if (hdq_data->init_trans == 0)
  559. omap_hdq_get(hdq_data);
  560. /*
  561. * We need to reset the slave before
  562. * issuing the SKIP ROM command, else
  563. * the slave will not work.
  564. */
  565. if (byte == W1_SKIP_ROM)
  566. omap_hdq_break(hdq_data);
  567. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  568. if (ret < 0) {
  569. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  570. return;
  571. }
  572. hdq_data->init_trans++;
  573. mutex_unlock(&hdq_data->hdq_mutex);
  574. ret = hdq_write_byte(hdq_data, byte, &status);
  575. if (ret < 0) {
  576. dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status);
  577. return;
  578. }
  579. /* Second write, data transferred. Release the module */
  580. if (hdq_data->init_trans > 1) {
  581. omap_hdq_put(hdq_data);
  582. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  583. if (ret < 0) {
  584. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  585. return;
  586. }
  587. hdq_data->init_trans = 0;
  588. mutex_unlock(&hdq_data->hdq_mutex);
  589. }
  590. }
  591. static int omap_hdq_probe(struct platform_device *pdev)
  592. {
  593. struct device *dev = &pdev->dev;
  594. struct hdq_data *hdq_data;
  595. struct resource *res;
  596. int ret, irq;
  597. u8 rev;
  598. const char *mode;
  599. hdq_data = devm_kzalloc(dev, sizeof(*hdq_data), GFP_KERNEL);
  600. if (!hdq_data) {
  601. dev_dbg(&pdev->dev, "unable to allocate memory\n");
  602. return -ENOMEM;
  603. }
  604. hdq_data->dev = dev;
  605. platform_set_drvdata(pdev, hdq_data);
  606. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  607. hdq_data->hdq_base = devm_ioremap_resource(dev, res);
  608. if (IS_ERR(hdq_data->hdq_base))
  609. return PTR_ERR(hdq_data->hdq_base);
  610. hdq_data->hdq_usecount = 0;
  611. hdq_data->rrw = 0;
  612. mutex_init(&hdq_data->hdq_mutex);
  613. pm_runtime_enable(&pdev->dev);
  614. ret = pm_runtime_get_sync(&pdev->dev);
  615. if (ret < 0) {
  616. dev_dbg(&pdev->dev, "pm_runtime_get_sync failed\n");
  617. goto err_w1;
  618. }
  619. ret = _omap_hdq_reset(hdq_data);
  620. if (ret) {
  621. dev_dbg(&pdev->dev, "reset failed\n");
  622. return -EINVAL;
  623. }
  624. rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION);
  625. dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n",
  626. (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt");
  627. spin_lock_init(&hdq_data->hdq_spinlock);
  628. irq = platform_get_irq(pdev, 0);
  629. if (irq < 0) {
  630. ret = -ENXIO;
  631. goto err_irq;
  632. }
  633. ret = devm_request_irq(dev, irq, hdq_isr, 0, "omap_hdq", hdq_data);
  634. if (ret < 0) {
  635. dev_dbg(&pdev->dev, "could not request irq\n");
  636. goto err_irq;
  637. }
  638. omap_hdq_break(hdq_data);
  639. pm_runtime_put_sync(&pdev->dev);
  640. ret = of_property_read_string(pdev->dev.of_node, "ti,mode", &mode);
  641. if (ret < 0 || !strcmp(mode, "hdq")) {
  642. hdq_data->mode = 0;
  643. omap_w1_master.search = omap_w1_search_bus;
  644. } else {
  645. hdq_data->mode = 1;
  646. omap_w1_master.triplet = omap_w1_triplet;
  647. }
  648. omap_w1_master.data = hdq_data;
  649. ret = w1_add_master_device(&omap_w1_master);
  650. if (ret) {
  651. dev_dbg(&pdev->dev, "Failure in registering w1 master\n");
  652. goto err_w1;
  653. }
  654. return 0;
  655. err_irq:
  656. pm_runtime_put_sync(&pdev->dev);
  657. err_w1:
  658. pm_runtime_disable(&pdev->dev);
  659. return ret;
  660. }
  661. static int omap_hdq_remove(struct platform_device *pdev)
  662. {
  663. struct hdq_data *hdq_data = platform_get_drvdata(pdev);
  664. mutex_lock(&hdq_data->hdq_mutex);
  665. if (hdq_data->hdq_usecount) {
  666. dev_dbg(&pdev->dev, "removed when use count is not zero\n");
  667. mutex_unlock(&hdq_data->hdq_mutex);
  668. return -EBUSY;
  669. }
  670. mutex_unlock(&hdq_data->hdq_mutex);
  671. /* remove module dependency */
  672. pm_runtime_disable(&pdev->dev);
  673. w1_remove_master_device(&omap_w1_master);
  674. return 0;
  675. }
  676. module_platform_driver(omap_hdq_driver);
  677. module_param(w1_id, int, S_IRUSR);
  678. MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection in HDQ mode");
  679. MODULE_AUTHOR("Texas Instruments");
  680. MODULE_DESCRIPTION("HDQ-1W driver Library");
  681. MODULE_LICENSE("GPL");