omap_hdq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 "../w1.h"
  21. #include "../w1_int.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_INTERRUPTMASK (1<<6)
  28. #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE (1<<5)
  29. #define OMAP_HDQ_CTRL_STATUS_GO (1<<4)
  30. #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION (1<<2)
  31. #define OMAP_HDQ_CTRL_STATUS_DIR (1<<1)
  32. #define OMAP_HDQ_CTRL_STATUS_MODE (1<<0)
  33. #define OMAP_HDQ_INT_STATUS 0x10
  34. #define OMAP_HDQ_INT_STATUS_TXCOMPLETE (1<<2)
  35. #define OMAP_HDQ_INT_STATUS_RXCOMPLETE (1<<1)
  36. #define OMAP_HDQ_INT_STATUS_TIMEOUT (1<<0)
  37. #define OMAP_HDQ_SYSCONFIG 0x14
  38. #define OMAP_HDQ_SYSCONFIG_SOFTRESET (1<<1)
  39. #define OMAP_HDQ_SYSCONFIG_AUTOIDLE (1<<0)
  40. #define OMAP_HDQ_SYSSTATUS 0x18
  41. #define OMAP_HDQ_SYSSTATUS_RESETDONE (1<<0)
  42. #define OMAP_HDQ_FLAG_CLEAR 0
  43. #define OMAP_HDQ_FLAG_SET 1
  44. #define OMAP_HDQ_TIMEOUT (HZ/5)
  45. #define OMAP_HDQ_MAX_USER 4
  46. static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue);
  47. static int w1_id;
  48. struct hdq_data {
  49. struct device *dev;
  50. void __iomem *hdq_base;
  51. /* lock status update */
  52. struct mutex hdq_mutex;
  53. int hdq_usecount;
  54. u8 hdq_irqstatus;
  55. /* device lock */
  56. spinlock_t hdq_spinlock;
  57. /*
  58. * Used to control the call to omap_hdq_get and omap_hdq_put.
  59. * HDQ Protocol: Write the CMD|REG_address first, followed by
  60. * the data wrire or read.
  61. */
  62. int init_trans;
  63. };
  64. static int omap_hdq_probe(struct platform_device *pdev);
  65. static int omap_hdq_remove(struct platform_device *pdev);
  66. static const struct of_device_id omap_hdq_dt_ids[] = {
  67. { .compatible = "ti,omap3-1w" },
  68. {}
  69. };
  70. MODULE_DEVICE_TABLE(of, omap_hdq_dt_ids);
  71. static struct platform_driver omap_hdq_driver = {
  72. .probe = omap_hdq_probe,
  73. .remove = omap_hdq_remove,
  74. .driver = {
  75. .name = "omap_hdq",
  76. .of_match_table = omap_hdq_dt_ids,
  77. },
  78. };
  79. static u8 omap_w1_read_byte(void *_hdq);
  80. static void omap_w1_write_byte(void *_hdq, u8 byte);
  81. static u8 omap_w1_reset_bus(void *_hdq);
  82. static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev,
  83. u8 search_type, w1_slave_found_callback slave_found);
  84. static struct w1_bus_master omap_w1_master = {
  85. .read_byte = omap_w1_read_byte,
  86. .write_byte = omap_w1_write_byte,
  87. .reset_bus = omap_w1_reset_bus,
  88. .search = omap_w1_search_bus,
  89. };
  90. /* HDQ register I/O routines */
  91. static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset)
  92. {
  93. return __raw_readl(hdq_data->hdq_base + offset);
  94. }
  95. static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val)
  96. {
  97. __raw_writel(val, hdq_data->hdq_base + offset);
  98. }
  99. static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset,
  100. u8 val, u8 mask)
  101. {
  102. u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask)
  103. | (val & mask);
  104. __raw_writel(new_val, hdq_data->hdq_base + offset);
  105. return new_val;
  106. }
  107. /*
  108. * Wait for one or more bits in flag change.
  109. * HDQ_FLAG_SET: wait until any bit in the flag is set.
  110. * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared.
  111. * return 0 on success and -ETIMEDOUT in the case of timeout.
  112. */
  113. static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset,
  114. u8 flag, u8 flag_set, u8 *status)
  115. {
  116. int ret = 0;
  117. unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
  118. if (flag_set == OMAP_HDQ_FLAG_CLEAR) {
  119. /* wait for the flag clear */
  120. while (((*status = hdq_reg_in(hdq_data, offset)) & flag)
  121. && time_before(jiffies, timeout)) {
  122. schedule_timeout_uninterruptible(1);
  123. }
  124. if (*status & flag)
  125. ret = -ETIMEDOUT;
  126. } else if (flag_set == OMAP_HDQ_FLAG_SET) {
  127. /* wait for the flag set */
  128. while (!((*status = hdq_reg_in(hdq_data, offset)) & flag)
  129. && time_before(jiffies, timeout)) {
  130. schedule_timeout_uninterruptible(1);
  131. }
  132. if (!(*status & flag))
  133. ret = -ETIMEDOUT;
  134. } else
  135. return -EINVAL;
  136. return ret;
  137. }
  138. /* write out a byte and fill *status with HDQ_INT_STATUS */
  139. static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status)
  140. {
  141. int ret;
  142. u8 tmp_status;
  143. unsigned long irqflags;
  144. *status = 0;
  145. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  146. /* clear interrupt flags via a dummy read */
  147. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  148. /* ISR loads it with new INT_STATUS */
  149. hdq_data->hdq_irqstatus = 0;
  150. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  151. hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val);
  152. /* set the GO bit */
  153. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO,
  154. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  155. /* wait for the TXCOMPLETE bit */
  156. ret = wait_event_timeout(hdq_wait_queue,
  157. hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
  158. if (ret == 0) {
  159. dev_dbg(hdq_data->dev, "TX wait elapsed\n");
  160. ret = -ETIMEDOUT;
  161. goto out;
  162. }
  163. *status = hdq_data->hdq_irqstatus;
  164. /* check irqstatus */
  165. if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) {
  166. dev_dbg(hdq_data->dev, "timeout waiting for"
  167. " TXCOMPLETE/RXCOMPLETE, %x", *status);
  168. ret = -ETIMEDOUT;
  169. goto out;
  170. }
  171. /* wait for the GO bit return to zero */
  172. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  173. OMAP_HDQ_CTRL_STATUS_GO,
  174. OMAP_HDQ_FLAG_CLEAR, &tmp_status);
  175. if (ret) {
  176. dev_dbg(hdq_data->dev, "timeout waiting GO bit"
  177. " return to zero, %x", tmp_status);
  178. }
  179. out:
  180. return ret;
  181. }
  182. /* HDQ Interrupt service routine */
  183. static irqreturn_t hdq_isr(int irq, void *_hdq)
  184. {
  185. struct hdq_data *hdq_data = _hdq;
  186. unsigned long irqflags;
  187. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  188. hdq_data->hdq_irqstatus = hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  189. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  190. dev_dbg(hdq_data->dev, "hdq_isr: %x", hdq_data->hdq_irqstatus);
  191. if (hdq_data->hdq_irqstatus &
  192. (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE
  193. | OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  194. /* wake up sleeping process */
  195. wake_up(&hdq_wait_queue);
  196. }
  197. return IRQ_HANDLED;
  198. }
  199. /* HDQ Mode: always return success */
  200. static u8 omap_w1_reset_bus(void *_hdq)
  201. {
  202. return 0;
  203. }
  204. /* W1 search callback function */
  205. static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev,
  206. u8 search_type, w1_slave_found_callback slave_found)
  207. {
  208. u64 module_id, rn_le, cs, id;
  209. if (w1_id)
  210. module_id = w1_id;
  211. else
  212. module_id = 0x1;
  213. rn_le = cpu_to_le64(module_id);
  214. /*
  215. * HDQ might not obey truly the 1-wire spec.
  216. * So calculate CRC based on module parameter.
  217. */
  218. cs = w1_calc_crc8((u8 *)&rn_le, 7);
  219. id = (cs << 56) | module_id;
  220. slave_found(master_dev, id);
  221. }
  222. static int _omap_hdq_reset(struct hdq_data *hdq_data)
  223. {
  224. int ret;
  225. u8 tmp_status;
  226. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, OMAP_HDQ_SYSCONFIG_SOFTRESET);
  227. /*
  228. * Select HDQ mode & enable clocks.
  229. * It is observed that INT flags can't be cleared via a read and GO/INIT
  230. * won't return to zero if interrupt is disabled. So we always enable
  231. * interrupt.
  232. */
  233. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  234. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  235. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
  236. /* wait for reset to complete */
  237. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_SYSSTATUS,
  238. OMAP_HDQ_SYSSTATUS_RESETDONE, OMAP_HDQ_FLAG_SET, &tmp_status);
  239. if (ret)
  240. dev_dbg(hdq_data->dev, "timeout waiting HDQ reset, %x",
  241. tmp_status);
  242. else {
  243. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  244. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  245. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
  246. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  247. OMAP_HDQ_SYSCONFIG_AUTOIDLE);
  248. }
  249. return ret;
  250. }
  251. /* Issue break pulse to the device */
  252. static int omap_hdq_break(struct hdq_data *hdq_data)
  253. {
  254. int ret = 0;
  255. u8 tmp_status;
  256. unsigned long irqflags;
  257. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  258. if (ret < 0) {
  259. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  260. ret = -EINTR;
  261. goto rtn;
  262. }
  263. spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
  264. /* clear interrupt flags via a dummy read */
  265. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  266. /* ISR loads it with new INT_STATUS */
  267. hdq_data->hdq_irqstatus = 0;
  268. spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
  269. /* set the INIT and GO bit */
  270. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  271. OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO,
  272. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  273. OMAP_HDQ_CTRL_STATUS_GO);
  274. /* wait for the TIMEOUT bit */
  275. ret = wait_event_timeout(hdq_wait_queue,
  276. hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
  277. if (ret == 0) {
  278. dev_dbg(hdq_data->dev, "break wait elapsed\n");
  279. ret = -EINTR;
  280. goto out;
  281. }
  282. tmp_status = hdq_data->hdq_irqstatus;
  283. /* check irqstatus */
  284. if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) {
  285. dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x",
  286. tmp_status);
  287. ret = -ETIMEDOUT;
  288. goto out;
  289. }
  290. /*
  291. * wait for both INIT and GO bits rerurn to zero.
  292. * zero wait time expected for interrupt mode.
  293. */
  294. ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
  295. OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
  296. OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR,
  297. &tmp_status);
  298. if (ret)
  299. dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits"
  300. " return to zero, %x", tmp_status);
  301. out:
  302. mutex_unlock(&hdq_data->hdq_mutex);
  303. rtn:
  304. return ret;
  305. }
  306. static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val)
  307. {
  308. int ret = 0;
  309. u8 status;
  310. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  311. if (ret < 0) {
  312. ret = -EINTR;
  313. goto rtn;
  314. }
  315. if (!hdq_data->hdq_usecount) {
  316. ret = -EINVAL;
  317. goto out;
  318. }
  319. if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  320. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
  321. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO,
  322. OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
  323. /*
  324. * The RX comes immediately after TX.
  325. */
  326. wait_event_timeout(hdq_wait_queue,
  327. (hdq_data->hdq_irqstatus
  328. & OMAP_HDQ_INT_STATUS_RXCOMPLETE),
  329. OMAP_HDQ_TIMEOUT);
  330. hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0,
  331. OMAP_HDQ_CTRL_STATUS_DIR);
  332. status = hdq_data->hdq_irqstatus;
  333. /* check irqstatus */
  334. if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
  335. dev_dbg(hdq_data->dev, "timeout waiting for"
  336. " RXCOMPLETE, %x", status);
  337. ret = -ETIMEDOUT;
  338. goto out;
  339. }
  340. }
  341. /* the data is ready. Read it in! */
  342. *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA);
  343. out:
  344. mutex_unlock(&hdq_data->hdq_mutex);
  345. rtn:
  346. return ret;
  347. }
  348. /* Enable clocks and set the controller to HDQ mode */
  349. static int omap_hdq_get(struct hdq_data *hdq_data)
  350. {
  351. int ret = 0;
  352. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  353. if (ret < 0) {
  354. ret = -EINTR;
  355. goto rtn;
  356. }
  357. if (OMAP_HDQ_MAX_USER == hdq_data->hdq_usecount) {
  358. dev_dbg(hdq_data->dev, "attempt to exceed the max use count");
  359. ret = -EINVAL;
  360. goto out;
  361. } else {
  362. hdq_data->hdq_usecount++;
  363. try_module_get(THIS_MODULE);
  364. if (1 == hdq_data->hdq_usecount) {
  365. pm_runtime_get_sync(hdq_data->dev);
  366. /* make sure HDQ is out of reset */
  367. if (!(hdq_reg_in(hdq_data, OMAP_HDQ_SYSSTATUS) &
  368. OMAP_HDQ_SYSSTATUS_RESETDONE)) {
  369. ret = _omap_hdq_reset(hdq_data);
  370. if (ret)
  371. /* back up the count */
  372. hdq_data->hdq_usecount--;
  373. } else {
  374. /* select HDQ mode & enable clocks */
  375. hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
  376. OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
  377. OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
  378. hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
  379. OMAP_HDQ_SYSCONFIG_AUTOIDLE);
  380. hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
  381. }
  382. }
  383. }
  384. out:
  385. mutex_unlock(&hdq_data->hdq_mutex);
  386. rtn:
  387. return ret;
  388. }
  389. /* Disable clocks to the module */
  390. static int omap_hdq_put(struct hdq_data *hdq_data)
  391. {
  392. int ret = 0;
  393. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  394. if (ret < 0)
  395. return -EINTR;
  396. if (0 == hdq_data->hdq_usecount) {
  397. dev_dbg(hdq_data->dev, "attempt to decrement use count"
  398. " when it is zero");
  399. ret = -EINVAL;
  400. } else {
  401. hdq_data->hdq_usecount--;
  402. module_put(THIS_MODULE);
  403. if (0 == hdq_data->hdq_usecount)
  404. pm_runtime_put_sync(hdq_data->dev);
  405. }
  406. mutex_unlock(&hdq_data->hdq_mutex);
  407. return ret;
  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 = hdq_read_byte(hdq_data, &val);
  416. if (ret) {
  417. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  418. if (ret < 0) {
  419. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  420. return -EINTR;
  421. }
  422. hdq_data->init_trans = 0;
  423. mutex_unlock(&hdq_data->hdq_mutex);
  424. omap_hdq_put(hdq_data);
  425. return -1;
  426. }
  427. /* Write followed by a read, release the module */
  428. if (hdq_data->init_trans) {
  429. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  430. if (ret < 0) {
  431. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  432. return -EINTR;
  433. }
  434. hdq_data->init_trans = 0;
  435. mutex_unlock(&hdq_data->hdq_mutex);
  436. omap_hdq_put(hdq_data);
  437. }
  438. return val;
  439. }
  440. /* Write a byte of data to the device */
  441. static void omap_w1_write_byte(void *_hdq, u8 byte)
  442. {
  443. struct hdq_data *hdq_data = _hdq;
  444. int ret;
  445. u8 status;
  446. /* First write to initialize the transfer */
  447. if (hdq_data->init_trans == 0)
  448. omap_hdq_get(hdq_data);
  449. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  450. if (ret < 0) {
  451. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  452. return;
  453. }
  454. hdq_data->init_trans++;
  455. mutex_unlock(&hdq_data->hdq_mutex);
  456. ret = hdq_write_byte(hdq_data, byte, &status);
  457. if (ret < 0) {
  458. dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status);
  459. return;
  460. }
  461. /* Second write, data transferred. Release the module */
  462. if (hdq_data->init_trans > 1) {
  463. omap_hdq_put(hdq_data);
  464. ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
  465. if (ret < 0) {
  466. dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
  467. return;
  468. }
  469. hdq_data->init_trans = 0;
  470. mutex_unlock(&hdq_data->hdq_mutex);
  471. }
  472. }
  473. static int omap_hdq_probe(struct platform_device *pdev)
  474. {
  475. struct device *dev = &pdev->dev;
  476. struct hdq_data *hdq_data;
  477. struct resource *res;
  478. int ret, irq;
  479. u8 rev;
  480. hdq_data = devm_kzalloc(dev, sizeof(*hdq_data), GFP_KERNEL);
  481. if (!hdq_data) {
  482. dev_dbg(&pdev->dev, "unable to allocate memory\n");
  483. return -ENOMEM;
  484. }
  485. hdq_data->dev = dev;
  486. platform_set_drvdata(pdev, hdq_data);
  487. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  488. hdq_data->hdq_base = devm_ioremap_resource(dev, res);
  489. if (IS_ERR(hdq_data->hdq_base))
  490. return PTR_ERR(hdq_data->hdq_base);
  491. hdq_data->hdq_usecount = 0;
  492. mutex_init(&hdq_data->hdq_mutex);
  493. pm_runtime_enable(&pdev->dev);
  494. pm_runtime_get_sync(&pdev->dev);
  495. rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION);
  496. dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n",
  497. (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt");
  498. spin_lock_init(&hdq_data->hdq_spinlock);
  499. irq = platform_get_irq(pdev, 0);
  500. if (irq < 0) {
  501. ret = -ENXIO;
  502. goto err_irq;
  503. }
  504. ret = devm_request_irq(dev, irq, hdq_isr, 0, "omap_hdq", hdq_data);
  505. if (ret < 0) {
  506. dev_dbg(&pdev->dev, "could not request irq\n");
  507. goto err_irq;
  508. }
  509. omap_hdq_break(hdq_data);
  510. pm_runtime_put_sync(&pdev->dev);
  511. omap_w1_master.data = hdq_data;
  512. ret = w1_add_master_device(&omap_w1_master);
  513. if (ret) {
  514. dev_dbg(&pdev->dev, "Failure in registering w1 master\n");
  515. goto err_w1;
  516. }
  517. return 0;
  518. err_irq:
  519. pm_runtime_put_sync(&pdev->dev);
  520. err_w1:
  521. pm_runtime_disable(&pdev->dev);
  522. return ret;
  523. }
  524. static int omap_hdq_remove(struct platform_device *pdev)
  525. {
  526. struct hdq_data *hdq_data = platform_get_drvdata(pdev);
  527. mutex_lock(&hdq_data->hdq_mutex);
  528. if (hdq_data->hdq_usecount) {
  529. dev_dbg(&pdev->dev, "removed when use count is not zero\n");
  530. mutex_unlock(&hdq_data->hdq_mutex);
  531. return -EBUSY;
  532. }
  533. mutex_unlock(&hdq_data->hdq_mutex);
  534. /* remove module dependency */
  535. pm_runtime_disable(&pdev->dev);
  536. return 0;
  537. }
  538. module_platform_driver(omap_hdq_driver);
  539. module_param(w1_id, int, S_IRUSR);
  540. MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection");
  541. MODULE_AUTHOR("Texas Instruments");
  542. MODULE_DESCRIPTION("HDQ driver Library");
  543. MODULE_LICENSE("GPL");