ucb1x00-core.c 19 KB

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