omap_hdq.c 16 KB

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