ucb1x00-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * linux/drivers/mfd/ucb1x00-core.c
  3. *
  4. * Copyright (C) 2001 Russell King, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License.
  9. *
  10. * The UCB1x00 core driver provides basic services for handling IO,
  11. * the ADC, interrupts, and accessing registers. It is designed
  12. * such that everything goes through this layer, thereby providing
  13. * a consistent locking methodology, as well as allowing the drivers
  14. * to be used on other non-MCP-enabled hardware platforms.
  15. *
  16. * Note that all locks are private to this file. Nothing else may
  17. * touch them.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/errno.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/device.h>
  27. #include <linux/mutex.h>
  28. #include <linux/mfd/ucb1x00.h>
  29. #include <linux/gpio.h>
  30. static DEFINE_MUTEX(ucb1x00_mutex);
  31. static LIST_HEAD(ucb1x00_drivers);
  32. static LIST_HEAD(ucb1x00_devices);
  33. /**
  34. * ucb1x00_io_set_dir - set IO direction
  35. * @ucb: UCB1x00 structure describing chip
  36. * @in: bitfield of IO pins to be set as inputs
  37. * @out: bitfield of IO pins to be set as outputs
  38. *
  39. * Set the IO direction of the ten general purpose IO pins on
  40. * the UCB1x00 chip. The @in bitfield has priority over the
  41. * @out bitfield, in that if you specify a pin as both input
  42. * and output, it will end up as an input.
  43. *
  44. * ucb1x00_enable must have been called to enable the comms
  45. * before using this function.
  46. *
  47. * This function takes a spinlock, disabling interrupts.
  48. */
  49. void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out)
  50. {
  51. unsigned long flags;
  52. spin_lock_irqsave(&ucb->io_lock, flags);
  53. ucb->io_dir |= out;
  54. ucb->io_dir &= ~in;
  55. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  56. spin_unlock_irqrestore(&ucb->io_lock, flags);
  57. }
  58. /**
  59. * ucb1x00_io_write - set or clear IO outputs
  60. * @ucb: UCB1x00 structure describing chip
  61. * @set: bitfield of IO pins to set to logic '1'
  62. * @clear: bitfield of IO pins to set to logic '0'
  63. *
  64. * Set the IO output state of the specified IO pins. The value
  65. * is retained if the pins are subsequently configured as inputs.
  66. * The @clear bitfield has priority over the @set bitfield -
  67. * outputs will be cleared.
  68. *
  69. * ucb1x00_enable must have been called to enable the comms
  70. * before using this function.
  71. *
  72. * This function takes a spinlock, disabling interrupts.
  73. */
  74. void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear)
  75. {
  76. unsigned long flags;
  77. spin_lock_irqsave(&ucb->io_lock, flags);
  78. ucb->io_out |= set;
  79. ucb->io_out &= ~clear;
  80. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  81. spin_unlock_irqrestore(&ucb->io_lock, flags);
  82. }
  83. /**
  84. * ucb1x00_io_read - read the current state of the IO pins
  85. * @ucb: UCB1x00 structure describing chip
  86. *
  87. * Return a bitfield describing the logic state of the ten
  88. * general purpose IO pins.
  89. *
  90. * ucb1x00_enable must have been called to enable the comms
  91. * before using this function.
  92. *
  93. * This function does not take any mutexes or spinlocks.
  94. */
  95. unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
  96. {
  97. return ucb1x00_reg_read(ucb, UCB_IO_DATA);
  98. }
  99. static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  100. {
  101. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  102. unsigned long flags;
  103. spin_lock_irqsave(&ucb->io_lock, flags);
  104. if (value)
  105. ucb->io_out |= 1 << offset;
  106. else
  107. ucb->io_out &= ~(1 << offset);
  108. ucb1x00_enable(ucb);
  109. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  110. ucb1x00_disable(ucb);
  111. spin_unlock_irqrestore(&ucb->io_lock, flags);
  112. }
  113. static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
  114. {
  115. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  116. unsigned val;
  117. ucb1x00_enable(ucb);
  118. val = ucb1x00_reg_read(ucb, UCB_IO_DATA);
  119. ucb1x00_disable(ucb);
  120. return val & (1 << offset);
  121. }
  122. static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  123. {
  124. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  125. unsigned long flags;
  126. spin_lock_irqsave(&ucb->io_lock, flags);
  127. ucb->io_dir &= ~(1 << offset);
  128. ucb1x00_enable(ucb);
  129. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  130. ucb1x00_disable(ucb);
  131. spin_unlock_irqrestore(&ucb->io_lock, flags);
  132. return 0;
  133. }
  134. static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
  135. , int value)
  136. {
  137. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  138. unsigned long flags;
  139. unsigned old, mask = 1 << offset;
  140. spin_lock_irqsave(&ucb->io_lock, flags);
  141. old = ucb->io_out;
  142. if (value)
  143. ucb->io_out |= mask;
  144. else
  145. ucb->io_out &= ~mask;
  146. ucb1x00_enable(ucb);
  147. if (old != ucb->io_out)
  148. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  149. if (!(ucb->io_dir & mask)) {
  150. ucb->io_dir |= mask;
  151. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  152. }
  153. ucb1x00_disable(ucb);
  154. spin_unlock_irqrestore(&ucb->io_lock, flags);
  155. return 0;
  156. }
  157. /*
  158. * UCB1300 data sheet says we must:
  159. * 1. enable ADC => 5us (including reference startup time)
  160. * 2. select input => 51*tsibclk => 4.3us
  161. * 3. start conversion => 102*tsibclk => 8.5us
  162. * (tsibclk = 1/11981000)
  163. * Period between SIB 128-bit frames = 10.7us
  164. */
  165. /**
  166. * ucb1x00_adc_enable - enable the ADC converter
  167. * @ucb: UCB1x00 structure describing chip
  168. *
  169. * Enable the ucb1x00 and ADC converter on the UCB1x00 for use.
  170. * Any code wishing to use the ADC converter must call this
  171. * function prior to using it.
  172. *
  173. * This function takes the ADC mutex to prevent two or more
  174. * concurrent uses, and therefore may sleep. As a result, it
  175. * can only be called from process context, not interrupt
  176. * context.
  177. *
  178. * You should release the ADC as soon as possible using
  179. * ucb1x00_adc_disable.
  180. */
  181. void ucb1x00_adc_enable(struct ucb1x00 *ucb)
  182. {
  183. mutex_lock(&ucb->adc_mutex);
  184. ucb->adc_cr |= UCB_ADC_ENA;
  185. ucb1x00_enable(ucb);
  186. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  187. }
  188. /**
  189. * ucb1x00_adc_read - read the specified ADC channel
  190. * @ucb: UCB1x00 structure describing chip
  191. * @adc_channel: ADC channel mask
  192. * @sync: wait for syncronisation pulse.
  193. *
  194. * Start an ADC conversion and wait for the result. Note that
  195. * synchronised ADC conversions (via the ADCSYNC pin) must wait
  196. * until the trigger is asserted and the conversion is finished.
  197. *
  198. * This function currently spins waiting for the conversion to
  199. * complete (2 frames max without sync).
  200. *
  201. * If called for a synchronised ADC conversion, it may sleep
  202. * with the ADC mutex held.
  203. */
  204. unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
  205. {
  206. unsigned int val;
  207. if (sync)
  208. adc_channel |= UCB_ADC_SYNC_ENA;
  209. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel);
  210. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START);
  211. for (;;) {
  212. val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
  213. if (val & UCB_ADC_DAT_VAL)
  214. break;
  215. /* yield to other processes */
  216. set_current_state(TASK_INTERRUPTIBLE);
  217. schedule_timeout(1);
  218. }
  219. return UCB_ADC_DAT(val);
  220. }
  221. /**
  222. * ucb1x00_adc_disable - disable the ADC converter
  223. * @ucb: UCB1x00 structure describing chip
  224. *
  225. * Disable the ADC converter and release the ADC mutex.
  226. */
  227. void ucb1x00_adc_disable(struct ucb1x00 *ucb)
  228. {
  229. ucb->adc_cr &= ~UCB_ADC_ENA;
  230. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  231. ucb1x00_disable(ucb);
  232. mutex_unlock(&ucb->adc_mutex);
  233. }
  234. /*
  235. * UCB1x00 Interrupt handling.
  236. *
  237. * The UCB1x00 can generate interrupts when the SIBCLK is stopped.
  238. * Since we need to read an internal register, we must re-enable
  239. * SIBCLK to talk to the chip. We leave the clock running until
  240. * we have finished processing all interrupts from the chip.
  241. */
  242. static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
  243. {
  244. struct ucb1x00 *ucb = devid;
  245. struct ucb1x00_irq *irq;
  246. unsigned int isr, i;
  247. ucb1x00_enable(ucb);
  248. isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS);
  249. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr);
  250. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  251. for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++)
  252. if (isr & 1 && irq->fn)
  253. irq->fn(i, irq->devid);
  254. ucb1x00_disable(ucb);
  255. return IRQ_HANDLED;
  256. }
  257. /**
  258. * ucb1x00_hook_irq - hook a UCB1x00 interrupt
  259. * @ucb: UCB1x00 structure describing chip
  260. * @idx: interrupt index
  261. * @fn: function to call when interrupt is triggered
  262. * @devid: device id to pass to interrupt handler
  263. *
  264. * Hook the specified interrupt. You can only register one handler
  265. * for each interrupt source. The interrupt source is not enabled
  266. * by this function; use ucb1x00_enable_irq instead.
  267. *
  268. * Interrupt handlers will be called with other interrupts enabled.
  269. *
  270. * Returns zero on success, or one of the following errors:
  271. * -EINVAL if the interrupt index is invalid
  272. * -EBUSY if the interrupt has already been hooked
  273. */
  274. int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid)
  275. {
  276. struct ucb1x00_irq *irq;
  277. int ret = -EINVAL;
  278. if (idx < 16) {
  279. irq = ucb->irq_handler + idx;
  280. ret = -EBUSY;
  281. spin_lock_irq(&ucb->lock);
  282. if (irq->fn == NULL) {
  283. irq->devid = devid;
  284. irq->fn = fn;
  285. ret = 0;
  286. }
  287. spin_unlock_irq(&ucb->lock);
  288. }
  289. return ret;
  290. }
  291. /**
  292. * ucb1x00_enable_irq - enable an UCB1x00 interrupt source
  293. * @ucb: UCB1x00 structure describing chip
  294. * @idx: interrupt index
  295. * @edges: interrupt edges to enable
  296. *
  297. * Enable the specified interrupt to trigger on %UCB_RISING,
  298. * %UCB_FALLING or both edges. The interrupt should have been
  299. * hooked by ucb1x00_hook_irq.
  300. */
  301. void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
  302. {
  303. unsigned long flags;
  304. if (idx < 16) {
  305. spin_lock_irqsave(&ucb->lock, flags);
  306. ucb1x00_enable(ucb);
  307. if (edges & UCB_RISING) {
  308. ucb->irq_ris_enbl |= 1 << idx;
  309. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  310. }
  311. if (edges & UCB_FALLING) {
  312. ucb->irq_fal_enbl |= 1 << idx;
  313. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  314. }
  315. ucb1x00_disable(ucb);
  316. spin_unlock_irqrestore(&ucb->lock, flags);
  317. }
  318. }
  319. /**
  320. * ucb1x00_disable_irq - disable an UCB1x00 interrupt source
  321. * @ucb: UCB1x00 structure describing chip
  322. * @edges: interrupt edges to disable
  323. *
  324. * Disable the specified interrupt triggering on the specified
  325. * (%UCB_RISING, %UCB_FALLING or both) edges.
  326. */
  327. void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
  328. {
  329. unsigned long flags;
  330. if (idx < 16) {
  331. spin_lock_irqsave(&ucb->lock, flags);
  332. ucb1x00_enable(ucb);
  333. if (edges & UCB_RISING) {
  334. ucb->irq_ris_enbl &= ~(1 << idx);
  335. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  336. }
  337. if (edges & UCB_FALLING) {
  338. ucb->irq_fal_enbl &= ~(1 << idx);
  339. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  340. }
  341. ucb1x00_disable(ucb);
  342. spin_unlock_irqrestore(&ucb->lock, flags);
  343. }
  344. }
  345. /**
  346. * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt
  347. * @ucb: UCB1x00 structure describing chip
  348. * @idx: interrupt index
  349. * @devid: device id.
  350. *
  351. * Disable the interrupt source and remove the handler. devid must
  352. * match the devid passed when hooking the interrupt.
  353. *
  354. * Returns zero on success, or one of the following errors:
  355. * -EINVAL if the interrupt index is invalid
  356. * -ENOENT if devid does not match
  357. */
  358. int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid)
  359. {
  360. struct ucb1x00_irq *irq;
  361. int ret;
  362. if (idx >= 16)
  363. goto bad;
  364. irq = ucb->irq_handler + idx;
  365. ret = -ENOENT;
  366. spin_lock_irq(&ucb->lock);
  367. if (irq->devid == devid) {
  368. ucb->irq_ris_enbl &= ~(1 << idx);
  369. ucb->irq_fal_enbl &= ~(1 << idx);
  370. ucb1x00_enable(ucb);
  371. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  372. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  373. ucb1x00_disable(ucb);
  374. irq->fn = NULL;
  375. irq->devid = NULL;
  376. ret = 0;
  377. }
  378. spin_unlock_irq(&ucb->lock);
  379. return ret;
  380. bad:
  381. printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx);
  382. return -EINVAL;
  383. }
  384. static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
  385. {
  386. struct ucb1x00_dev *dev;
  387. int ret = -ENOMEM;
  388. dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
  389. if (dev) {
  390. dev->ucb = ucb;
  391. dev->drv = drv;
  392. ret = drv->add(dev);
  393. if (ret == 0) {
  394. list_add_tail(&dev->dev_node, &ucb->devs);
  395. list_add_tail(&dev->drv_node, &drv->devs);
  396. } else {
  397. kfree(dev);
  398. }
  399. }
  400. return ret;
  401. }
  402. static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
  403. {
  404. dev->drv->remove(dev);
  405. list_del(&dev->dev_node);
  406. list_del(&dev->drv_node);
  407. kfree(dev);
  408. }
  409. /*
  410. * Try to probe our interrupt, rather than relying on lots of
  411. * hard-coded machine dependencies. For reference, the expected
  412. * IRQ mappings are:
  413. *
  414. * Machine Default IRQ
  415. * adsbitsy IRQ_GPCIN4
  416. * cerf IRQ_GPIO_UCB1200_IRQ
  417. * flexanet IRQ_GPIO_GUI
  418. * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ
  419. * graphicsclient ADS_EXT_IRQ(8)
  420. * graphicsmaster ADS_EXT_IRQ(8)
  421. * lart LART_IRQ_UCB1200
  422. * omnimeter IRQ_GPIO23
  423. * pfs168 IRQ_GPIO_UCB1300_IRQ
  424. * simpad IRQ_GPIO_UCB1300_IRQ
  425. * shannon SHANNON_IRQ_GPIO_IRQ_CODEC
  426. * yopy IRQ_GPIO_UCB1200_IRQ
  427. */
  428. static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
  429. {
  430. unsigned long mask;
  431. mask = probe_irq_on();
  432. if (!mask) {
  433. probe_irq_off(mask);
  434. return NO_IRQ;
  435. }
  436. /*
  437. * Enable the ADC interrupt.
  438. */
  439. ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC);
  440. ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC);
  441. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  442. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  443. /*
  444. * Cause an ADC interrupt.
  445. */
  446. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA);
  447. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
  448. /*
  449. * Wait for the conversion to complete.
  450. */
  451. while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
  452. ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
  453. /*
  454. * Disable and clear interrupt.
  455. */
  456. ucb1x00_reg_write(ucb, UCB_IE_RIS, 0);
  457. ucb1x00_reg_write(ucb, UCB_IE_FAL, 0);
  458. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  459. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  460. /*
  461. * Read triggered interrupt.
  462. */
  463. return probe_irq_off(mask);
  464. }
  465. static void ucb1x00_release(struct device *dev)
  466. {
  467. struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
  468. kfree(ucb);
  469. }
  470. static struct class ucb1x00_class = {
  471. .name = "ucb1x00",
  472. .dev_release = ucb1x00_release,
  473. };
  474. static int ucb1x00_probe(struct mcp *mcp)
  475. {
  476. struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data;
  477. struct ucb1x00_driver *drv;
  478. struct ucb1x00 *ucb;
  479. unsigned int id;
  480. int ret = -ENODEV;
  481. int temp;
  482. /* Tell the platform to deassert the UCB1x00 reset */
  483. if (pdata && pdata->reset)
  484. pdata->reset(UCB_RST_PROBE);
  485. mcp_enable(mcp);
  486. id = mcp_reg_read(mcp, UCB_ID);
  487. mcp_disable(mcp);
  488. if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
  489. printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
  490. goto out;
  491. }
  492. ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
  493. ret = -ENOMEM;
  494. if (!ucb)
  495. goto out;
  496. device_initialize(&ucb->dev);
  497. ucb->dev.class = &ucb1x00_class;
  498. ucb->dev.parent = &mcp->attached_device;
  499. dev_set_name(&ucb->dev, "ucb1x00");
  500. spin_lock_init(&ucb->lock);
  501. spin_lock_init(&ucb->io_lock);
  502. mutex_init(&ucb->adc_mutex);
  503. ucb->id = id;
  504. ucb->mcp = mcp;
  505. ret = device_add(&ucb->dev);
  506. if (ret)
  507. goto err_dev_add;
  508. ucb1x00_enable(ucb);
  509. ucb->irq = ucb1x00_detect_irq(ucb);
  510. ucb1x00_disable(ucb);
  511. if (ucb->irq == NO_IRQ) {
  512. dev_err(&ucb->dev, "IRQ probe failed\n");
  513. ret = -ENODEV;
  514. goto err_no_irq;
  515. }
  516. ucb->gpio.base = -1;
  517. if (pdata && pdata->gpio_base) {
  518. ucb->gpio.label = dev_name(&ucb->dev);
  519. ucb->gpio.dev = &ucb->dev;
  520. ucb->gpio.owner = THIS_MODULE;
  521. ucb->gpio.base = pdata->gpio_base;
  522. ucb->gpio.ngpio = 10;
  523. ucb->gpio.set = ucb1x00_gpio_set;
  524. ucb->gpio.get = ucb1x00_gpio_get;
  525. ucb->gpio.direction_input = ucb1x00_gpio_direction_input;
  526. ucb->gpio.direction_output = ucb1x00_gpio_direction_output;
  527. ret = gpiochip_add(&ucb->gpio);
  528. if (ret)
  529. goto err_gpio_add;
  530. } else
  531. dev_info(&ucb->dev, "gpio_base not set so no gpiolib support");
  532. ret = request_irq(ucb->irq, ucb1x00_irq, IRQF_TRIGGER_RISING,
  533. "UCB1x00", ucb);
  534. if (ret) {
  535. dev_err(&ucb->dev, "ucb1x00: unable to grab irq%d: %d\n",
  536. ucb->irq, ret);
  537. goto err_irq;
  538. }
  539. mcp_set_drvdata(mcp, ucb);
  540. INIT_LIST_HEAD(&ucb->devs);
  541. mutex_lock(&ucb1x00_mutex);
  542. list_add_tail(&ucb->node, &ucb1x00_devices);
  543. list_for_each_entry(drv, &ucb1x00_drivers, node) {
  544. ucb1x00_add_dev(ucb, drv);
  545. }
  546. mutex_unlock(&ucb1x00_mutex);
  547. return ret;
  548. err_irq:
  549. if (ucb->gpio.base != -1)
  550. temp = gpiochip_remove(&ucb->gpio);
  551. err_gpio_add:
  552. err_no_irq:
  553. device_del(&ucb->dev);
  554. err_dev_add:
  555. put_device(&ucb->dev);
  556. out:
  557. if (pdata && pdata->reset)
  558. pdata->reset(UCB_RST_PROBE_FAIL);
  559. return ret;
  560. }
  561. static void ucb1x00_remove(struct mcp *mcp)
  562. {
  563. struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data;
  564. struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
  565. struct list_head *l, *n;
  566. int ret;
  567. mutex_lock(&ucb1x00_mutex);
  568. list_del(&ucb->node);
  569. list_for_each_safe(l, n, &ucb->devs) {
  570. struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node);
  571. ucb1x00_remove_dev(dev);
  572. }
  573. mutex_unlock(&ucb1x00_mutex);
  574. if (ucb->gpio.base != -1) {
  575. ret = gpiochip_remove(&ucb->gpio);
  576. if (ret)
  577. dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret);
  578. }
  579. free_irq(ucb->irq, ucb);
  580. device_unregister(&ucb->dev);
  581. if (pdata && pdata->reset)
  582. pdata->reset(UCB_RST_REMOVE);
  583. }
  584. int ucb1x00_register_driver(struct ucb1x00_driver *drv)
  585. {
  586. struct ucb1x00 *ucb;
  587. INIT_LIST_HEAD(&drv->devs);
  588. mutex_lock(&ucb1x00_mutex);
  589. list_add_tail(&drv->node, &ucb1x00_drivers);
  590. list_for_each_entry(ucb, &ucb1x00_devices, node) {
  591. ucb1x00_add_dev(ucb, drv);
  592. }
  593. mutex_unlock(&ucb1x00_mutex);
  594. return 0;
  595. }
  596. void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
  597. {
  598. struct list_head *n, *l;
  599. mutex_lock(&ucb1x00_mutex);
  600. list_del(&drv->node);
  601. list_for_each_safe(l, n, &drv->devs) {
  602. struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node);
  603. ucb1x00_remove_dev(dev);
  604. }
  605. mutex_unlock(&ucb1x00_mutex);
  606. }
  607. static int ucb1x00_suspend(struct mcp *mcp, pm_message_t state)
  608. {
  609. struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
  610. struct ucb1x00_dev *dev;
  611. mutex_lock(&ucb1x00_mutex);
  612. list_for_each_entry(dev, &ucb->devs, dev_node) {
  613. if (dev->drv->suspend)
  614. dev->drv->suspend(dev, state);
  615. }
  616. mutex_unlock(&ucb1x00_mutex);
  617. return 0;
  618. }
  619. static int ucb1x00_resume(struct mcp *mcp)
  620. {
  621. struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
  622. struct ucb1x00_dev *dev;
  623. ucb1x00_enable(ucb);
  624. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  625. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  626. ucb1x00_disable(ucb);
  627. mutex_lock(&ucb1x00_mutex);
  628. list_for_each_entry(dev, &ucb->devs, dev_node) {
  629. if (dev->drv->resume)
  630. dev->drv->resume(dev);
  631. }
  632. mutex_unlock(&ucb1x00_mutex);
  633. return 0;
  634. }
  635. static struct mcp_driver ucb1x00_driver = {
  636. .drv = {
  637. .name = "ucb1x00",
  638. .owner = THIS_MODULE,
  639. },
  640. .probe = ucb1x00_probe,
  641. .remove = ucb1x00_remove,
  642. .suspend = ucb1x00_suspend,
  643. .resume = ucb1x00_resume,
  644. };
  645. static int __init ucb1x00_init(void)
  646. {
  647. int ret = class_register(&ucb1x00_class);
  648. if (ret == 0) {
  649. ret = mcp_driver_register(&ucb1x00_driver);
  650. if (ret)
  651. class_unregister(&ucb1x00_class);
  652. }
  653. return ret;
  654. }
  655. static void __exit ucb1x00_exit(void)
  656. {
  657. mcp_driver_unregister(&ucb1x00_driver);
  658. class_unregister(&ucb1x00_class);
  659. }
  660. module_init(ucb1x00_init);
  661. module_exit(ucb1x00_exit);
  662. EXPORT_SYMBOL(ucb1x00_io_set_dir);
  663. EXPORT_SYMBOL(ucb1x00_io_write);
  664. EXPORT_SYMBOL(ucb1x00_io_read);
  665. EXPORT_SYMBOL(ucb1x00_adc_enable);
  666. EXPORT_SYMBOL(ucb1x00_adc_read);
  667. EXPORT_SYMBOL(ucb1x00_adc_disable);
  668. EXPORT_SYMBOL(ucb1x00_hook_irq);
  669. EXPORT_SYMBOL(ucb1x00_free_irq);
  670. EXPORT_SYMBOL(ucb1x00_enable_irq);
  671. EXPORT_SYMBOL(ucb1x00_disable_irq);
  672. EXPORT_SYMBOL(ucb1x00_register_driver);
  673. EXPORT_SYMBOL(ucb1x00_unregister_driver);
  674. MODULE_ALIAS("mcp:ucb1x00");
  675. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  676. MODULE_DESCRIPTION("UCB1x00 core driver");
  677. MODULE_LICENSE("GPL");