ucb1x00-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. static DEFINE_MUTEX(ucb1x00_mutex);
  32. static LIST_HEAD(ucb1x00_drivers);
  33. static LIST_HEAD(ucb1x00_devices);
  34. /**
  35. * ucb1x00_io_set_dir - set IO direction
  36. * @ucb: UCB1x00 structure describing chip
  37. * @in: bitfield of IO pins to be set as inputs
  38. * @out: bitfield of IO pins to be set as outputs
  39. *
  40. * Set the IO direction of the ten general purpose IO pins on
  41. * the UCB1x00 chip. The @in bitfield has priority over the
  42. * @out bitfield, in that if you specify a pin as both input
  43. * and output, it will end up as an input.
  44. *
  45. * ucb1x00_enable must have been called to enable the comms
  46. * before using this function.
  47. *
  48. * This function takes a spinlock, disabling interrupts.
  49. */
  50. void ucb1x00_io_set_dir(struct ucb1x00 *ucb, unsigned int in, unsigned int out)
  51. {
  52. unsigned long flags;
  53. spin_lock_irqsave(&ucb->io_lock, flags);
  54. ucb->io_dir |= out;
  55. ucb->io_dir &= ~in;
  56. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  57. spin_unlock_irqrestore(&ucb->io_lock, flags);
  58. }
  59. /**
  60. * ucb1x00_io_write - set or clear IO outputs
  61. * @ucb: UCB1x00 structure describing chip
  62. * @set: bitfield of IO pins to set to logic '1'
  63. * @clear: bitfield of IO pins to set to logic '0'
  64. *
  65. * Set the IO output state of the specified IO pins. The value
  66. * is retained if the pins are subsequently configured as inputs.
  67. * The @clear bitfield has priority over the @set bitfield -
  68. * outputs will be cleared.
  69. *
  70. * ucb1x00_enable must have been called to enable the comms
  71. * before using this function.
  72. *
  73. * This function takes a spinlock, disabling interrupts.
  74. */
  75. void ucb1x00_io_write(struct ucb1x00 *ucb, unsigned int set, unsigned int clear)
  76. {
  77. unsigned long flags;
  78. spin_lock_irqsave(&ucb->io_lock, flags);
  79. ucb->io_out |= set;
  80. ucb->io_out &= ~clear;
  81. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  82. spin_unlock_irqrestore(&ucb->io_lock, flags);
  83. }
  84. /**
  85. * ucb1x00_io_read - read the current state of the IO pins
  86. * @ucb: UCB1x00 structure describing chip
  87. *
  88. * Return a bitfield describing the logic state of the ten
  89. * general purpose IO pins.
  90. *
  91. * ucb1x00_enable must have been called to enable the comms
  92. * before using this function.
  93. *
  94. * This function does not take any semaphores or spinlocks.
  95. */
  96. unsigned int ucb1x00_io_read(struct ucb1x00 *ucb)
  97. {
  98. return ucb1x00_reg_read(ucb, UCB_IO_DATA);
  99. }
  100. static void ucb1x00_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  101. {
  102. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  103. unsigned long flags;
  104. spin_lock_irqsave(&ucb->io_lock, flags);
  105. if (value)
  106. ucb->io_out |= 1 << offset;
  107. else
  108. ucb->io_out &= ~(1 << offset);
  109. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  110. spin_unlock_irqrestore(&ucb->io_lock, flags);
  111. }
  112. static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
  113. {
  114. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  115. return ucb1x00_reg_read(ucb, UCB_IO_DATA) & (1 << offset);
  116. }
  117. static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  118. {
  119. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  120. unsigned long flags;
  121. spin_lock_irqsave(&ucb->io_lock, flags);
  122. ucb->io_dir &= ~(1 << offset);
  123. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  124. spin_unlock_irqrestore(&ucb->io_lock, flags);
  125. return 0;
  126. }
  127. static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
  128. , int value)
  129. {
  130. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  131. unsigned long flags;
  132. unsigned old, mask = 1 << offset;
  133. spin_lock_irqsave(&ucb->io_lock, flags);
  134. old = ucb->io_out;
  135. if (value)
  136. ucb->io_out |= mask;
  137. else
  138. ucb->io_out &= ~mask;
  139. if (old != ucb->io_out)
  140. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  141. if (!(ucb->io_dir & mask)) {
  142. ucb->io_dir |= mask;
  143. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  144. }
  145. spin_unlock_irqrestore(&ucb->io_lock, flags);
  146. return 0;
  147. }
  148. /*
  149. * UCB1300 data sheet says we must:
  150. * 1. enable ADC => 5us (including reference startup time)
  151. * 2. select input => 51*tsibclk => 4.3us
  152. * 3. start conversion => 102*tsibclk => 8.5us
  153. * (tsibclk = 1/11981000)
  154. * Period between SIB 128-bit frames = 10.7us
  155. */
  156. /**
  157. * ucb1x00_adc_enable - enable the ADC converter
  158. * @ucb: UCB1x00 structure describing chip
  159. *
  160. * Enable the ucb1x00 and ADC converter on the UCB1x00 for use.
  161. * Any code wishing to use the ADC converter must call this
  162. * function prior to using it.
  163. *
  164. * This function takes the ADC semaphore to prevent two or more
  165. * concurrent uses, and therefore may sleep. As a result, it
  166. * can only be called from process context, not interrupt
  167. * context.
  168. *
  169. * You should release the ADC as soon as possible using
  170. * ucb1x00_adc_disable.
  171. */
  172. void ucb1x00_adc_enable(struct ucb1x00 *ucb)
  173. {
  174. down(&ucb->adc_sem);
  175. ucb->adc_cr |= UCB_ADC_ENA;
  176. ucb1x00_enable(ucb);
  177. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  178. }
  179. /**
  180. * ucb1x00_adc_read - read the specified ADC channel
  181. * @ucb: UCB1x00 structure describing chip
  182. * @adc_channel: ADC channel mask
  183. * @sync: wait for syncronisation pulse.
  184. *
  185. * Start an ADC conversion and wait for the result. Note that
  186. * synchronised ADC conversions (via the ADCSYNC pin) must wait
  187. * until the trigger is asserted and the conversion is finished.
  188. *
  189. * This function currently spins waiting for the conversion to
  190. * complete (2 frames max without sync).
  191. *
  192. * If called for a synchronised ADC conversion, it may sleep
  193. * with the ADC semaphore held.
  194. */
  195. unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
  196. {
  197. unsigned int val;
  198. if (sync)
  199. adc_channel |= UCB_ADC_SYNC_ENA;
  200. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel);
  201. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START);
  202. for (;;) {
  203. val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
  204. if (val & UCB_ADC_DAT_VAL)
  205. break;
  206. /* yield to other processes */
  207. set_current_state(TASK_INTERRUPTIBLE);
  208. schedule_timeout(1);
  209. }
  210. return UCB_ADC_DAT(val);
  211. }
  212. /**
  213. * ucb1x00_adc_disable - disable the ADC converter
  214. * @ucb: UCB1x00 structure describing chip
  215. *
  216. * Disable the ADC converter and release the ADC semaphore.
  217. */
  218. void ucb1x00_adc_disable(struct ucb1x00 *ucb)
  219. {
  220. ucb->adc_cr &= ~UCB_ADC_ENA;
  221. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  222. ucb1x00_disable(ucb);
  223. up(&ucb->adc_sem);
  224. }
  225. /*
  226. * UCB1x00 Interrupt handling.
  227. *
  228. * The UCB1x00 can generate interrupts when the SIBCLK is stopped.
  229. * Since we need to read an internal register, we must re-enable
  230. * SIBCLK to talk to the chip. We leave the clock running until
  231. * we have finished processing all interrupts from the chip.
  232. */
  233. static irqreturn_t ucb1x00_irq(int irqnr, void *devid)
  234. {
  235. struct ucb1x00 *ucb = devid;
  236. struct ucb1x00_irq *irq;
  237. unsigned int isr, i;
  238. ucb1x00_enable(ucb);
  239. isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS);
  240. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr);
  241. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  242. for (i = 0, irq = ucb->irq_handler; i < 16 && isr; i++, isr >>= 1, irq++)
  243. if (isr & 1 && irq->fn)
  244. irq->fn(i, irq->devid);
  245. ucb1x00_disable(ucb);
  246. return IRQ_HANDLED;
  247. }
  248. /**
  249. * ucb1x00_hook_irq - hook a UCB1x00 interrupt
  250. * @ucb: UCB1x00 structure describing chip
  251. * @idx: interrupt index
  252. * @fn: function to call when interrupt is triggered
  253. * @devid: device id to pass to interrupt handler
  254. *
  255. * Hook the specified interrupt. You can only register one handler
  256. * for each interrupt source. The interrupt source is not enabled
  257. * by this function; use ucb1x00_enable_irq instead.
  258. *
  259. * Interrupt handlers will be called with other interrupts enabled.
  260. *
  261. * Returns zero on success, or one of the following errors:
  262. * -EINVAL if the interrupt index is invalid
  263. * -EBUSY if the interrupt has already been hooked
  264. */
  265. int ucb1x00_hook_irq(struct ucb1x00 *ucb, unsigned int idx, void (*fn)(int, void *), void *devid)
  266. {
  267. struct ucb1x00_irq *irq;
  268. int ret = -EINVAL;
  269. if (idx < 16) {
  270. irq = ucb->irq_handler + idx;
  271. ret = -EBUSY;
  272. spin_lock_irq(&ucb->lock);
  273. if (irq->fn == NULL) {
  274. irq->devid = devid;
  275. irq->fn = fn;
  276. ret = 0;
  277. }
  278. spin_unlock_irq(&ucb->lock);
  279. }
  280. return ret;
  281. }
  282. /**
  283. * ucb1x00_enable_irq - enable an UCB1x00 interrupt source
  284. * @ucb: UCB1x00 structure describing chip
  285. * @idx: interrupt index
  286. * @edges: interrupt edges to enable
  287. *
  288. * Enable the specified interrupt to trigger on %UCB_RISING,
  289. * %UCB_FALLING or both edges. The interrupt should have been
  290. * hooked by ucb1x00_hook_irq.
  291. */
  292. void ucb1x00_enable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
  293. {
  294. unsigned long flags;
  295. if (idx < 16) {
  296. spin_lock_irqsave(&ucb->lock, flags);
  297. ucb1x00_enable(ucb);
  298. if (edges & UCB_RISING) {
  299. ucb->irq_ris_enbl |= 1 << idx;
  300. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  301. }
  302. if (edges & UCB_FALLING) {
  303. ucb->irq_fal_enbl |= 1 << idx;
  304. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  305. }
  306. ucb1x00_disable(ucb);
  307. spin_unlock_irqrestore(&ucb->lock, flags);
  308. }
  309. }
  310. /**
  311. * ucb1x00_disable_irq - disable an UCB1x00 interrupt source
  312. * @ucb: UCB1x00 structure describing chip
  313. * @edges: interrupt edges to disable
  314. *
  315. * Disable the specified interrupt triggering on the specified
  316. * (%UCB_RISING, %UCB_FALLING or both) edges.
  317. */
  318. void ucb1x00_disable_irq(struct ucb1x00 *ucb, unsigned int idx, int edges)
  319. {
  320. unsigned long flags;
  321. if (idx < 16) {
  322. spin_lock_irqsave(&ucb->lock, flags);
  323. ucb1x00_enable(ucb);
  324. if (edges & UCB_RISING) {
  325. ucb->irq_ris_enbl &= ~(1 << idx);
  326. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  327. }
  328. if (edges & UCB_FALLING) {
  329. ucb->irq_fal_enbl &= ~(1 << idx);
  330. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  331. }
  332. ucb1x00_disable(ucb);
  333. spin_unlock_irqrestore(&ucb->lock, flags);
  334. }
  335. }
  336. /**
  337. * ucb1x00_free_irq - disable and free the specified UCB1x00 interrupt
  338. * @ucb: UCB1x00 structure describing chip
  339. * @idx: interrupt index
  340. * @devid: device id.
  341. *
  342. * Disable the interrupt source and remove the handler. devid must
  343. * match the devid passed when hooking the interrupt.
  344. *
  345. * Returns zero on success, or one of the following errors:
  346. * -EINVAL if the interrupt index is invalid
  347. * -ENOENT if devid does not match
  348. */
  349. int ucb1x00_free_irq(struct ucb1x00 *ucb, unsigned int idx, void *devid)
  350. {
  351. struct ucb1x00_irq *irq;
  352. int ret;
  353. if (idx >= 16)
  354. goto bad;
  355. irq = ucb->irq_handler + idx;
  356. ret = -ENOENT;
  357. spin_lock_irq(&ucb->lock);
  358. if (irq->devid == devid) {
  359. ucb->irq_ris_enbl &= ~(1 << idx);
  360. ucb->irq_fal_enbl &= ~(1 << idx);
  361. ucb1x00_enable(ucb);
  362. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl);
  363. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl);
  364. ucb1x00_disable(ucb);
  365. irq->fn = NULL;
  366. irq->devid = NULL;
  367. ret = 0;
  368. }
  369. spin_unlock_irq(&ucb->lock);
  370. return ret;
  371. bad:
  372. printk(KERN_ERR "Freeing bad UCB1x00 irq %d\n", idx);
  373. return -EINVAL;
  374. }
  375. static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
  376. {
  377. struct ucb1x00_dev *dev;
  378. int ret = -ENOMEM;
  379. dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
  380. if (dev) {
  381. dev->ucb = ucb;
  382. dev->drv = drv;
  383. ret = drv->add(dev);
  384. if (ret == 0) {
  385. list_add(&dev->dev_node, &ucb->devs);
  386. list_add(&dev->drv_node, &drv->devs);
  387. } else {
  388. kfree(dev);
  389. }
  390. }
  391. return ret;
  392. }
  393. static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
  394. {
  395. dev->drv->remove(dev);
  396. list_del(&dev->dev_node);
  397. list_del(&dev->drv_node);
  398. kfree(dev);
  399. }
  400. /*
  401. * Try to probe our interrupt, rather than relying on lots of
  402. * hard-coded machine dependencies. For reference, the expected
  403. * IRQ mappings are:
  404. *
  405. * Machine Default IRQ
  406. * adsbitsy IRQ_GPCIN4
  407. * cerf IRQ_GPIO_UCB1200_IRQ
  408. * flexanet IRQ_GPIO_GUI
  409. * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ
  410. * graphicsclient ADS_EXT_IRQ(8)
  411. * graphicsmaster ADS_EXT_IRQ(8)
  412. * lart LART_IRQ_UCB1200
  413. * omnimeter IRQ_GPIO23
  414. * pfs168 IRQ_GPIO_UCB1300_IRQ
  415. * simpad IRQ_GPIO_UCB1300_IRQ
  416. * shannon SHANNON_IRQ_GPIO_IRQ_CODEC
  417. * yopy IRQ_GPIO_UCB1200_IRQ
  418. */
  419. static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
  420. {
  421. unsigned long mask;
  422. mask = probe_irq_on();
  423. if (!mask) {
  424. probe_irq_off(mask);
  425. return NO_IRQ;
  426. }
  427. /*
  428. * Enable the ADC interrupt.
  429. */
  430. ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC);
  431. ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC);
  432. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  433. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  434. /*
  435. * Cause an ADC interrupt.
  436. */
  437. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA);
  438. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
  439. /*
  440. * Wait for the conversion to complete.
  441. */
  442. while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
  443. ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
  444. /*
  445. * Disable and clear interrupt.
  446. */
  447. ucb1x00_reg_write(ucb, UCB_IE_RIS, 0);
  448. ucb1x00_reg_write(ucb, UCB_IE_FAL, 0);
  449. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  450. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  451. /*
  452. * Read triggered interrupt.
  453. */
  454. return probe_irq_off(mask);
  455. }
  456. static void ucb1x00_release(struct device *dev)
  457. {
  458. struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
  459. kfree(ucb);
  460. }
  461. static struct class ucb1x00_class = {
  462. .name = "ucb1x00",
  463. .dev_release = ucb1x00_release,
  464. };
  465. static int ucb1x00_probe(struct mcp *mcp)
  466. {
  467. struct ucb1x00 *ucb;
  468. struct ucb1x00_driver *drv;
  469. struct ucb1x00_plat_data *pdata;
  470. unsigned int id;
  471. int ret = -ENODEV;
  472. int temp;
  473. mcp_enable(mcp);
  474. id = mcp_reg_read(mcp, UCB_ID);
  475. if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
  476. printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
  477. goto err_disable;
  478. }
  479. ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
  480. ret = -ENOMEM;
  481. if (!ucb)
  482. goto err_disable;
  483. pdata = mcp->attached_device.platform_data;
  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 (pdata && pdata->gpio_base) {
  500. ucb->gpio.label = dev_name(&ucb->dev);
  501. ucb->gpio.base = pdata->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");