ucb1x00-core.c 19 KB

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