ucb1x00-core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/irq.h>
  27. #include <linux/device.h>
  28. #include <linux/mutex.h>
  29. #include <linux/mfd/ucb1x00.h>
  30. #include <linux/pm.h>
  31. #include <linux/gpio.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 mutexes 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_enable(ucb);
  111. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  112. ucb1x00_disable(ucb);
  113. spin_unlock_irqrestore(&ucb->io_lock, flags);
  114. }
  115. static int ucb1x00_gpio_get(struct gpio_chip *chip, unsigned offset)
  116. {
  117. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  118. unsigned val;
  119. ucb1x00_enable(ucb);
  120. val = ucb1x00_reg_read(ucb, UCB_IO_DATA);
  121. ucb1x00_disable(ucb);
  122. return val & (1 << offset);
  123. }
  124. static int ucb1x00_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  125. {
  126. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  127. unsigned long flags;
  128. spin_lock_irqsave(&ucb->io_lock, flags);
  129. ucb->io_dir &= ~(1 << offset);
  130. ucb1x00_enable(ucb);
  131. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  132. ucb1x00_disable(ucb);
  133. spin_unlock_irqrestore(&ucb->io_lock, flags);
  134. return 0;
  135. }
  136. static int ucb1x00_gpio_direction_output(struct gpio_chip *chip, unsigned offset
  137. , int value)
  138. {
  139. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  140. unsigned long flags;
  141. unsigned old, mask = 1 << offset;
  142. spin_lock_irqsave(&ucb->io_lock, flags);
  143. old = ucb->io_out;
  144. if (value)
  145. ucb->io_out |= mask;
  146. else
  147. ucb->io_out &= ~mask;
  148. ucb1x00_enable(ucb);
  149. if (old != ucb->io_out)
  150. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  151. if (!(ucb->io_dir & mask)) {
  152. ucb->io_dir |= mask;
  153. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  154. }
  155. ucb1x00_disable(ucb);
  156. spin_unlock_irqrestore(&ucb->io_lock, flags);
  157. return 0;
  158. }
  159. static int ucb1x00_to_irq(struct gpio_chip *chip, unsigned offset)
  160. {
  161. struct ucb1x00 *ucb = container_of(chip, struct ucb1x00, gpio);
  162. return ucb->irq_base > 0 ? ucb->irq_base + offset : -ENXIO;
  163. }
  164. /*
  165. * UCB1300 data sheet says we must:
  166. * 1. enable ADC => 5us (including reference startup time)
  167. * 2. select input => 51*tsibclk => 4.3us
  168. * 3. start conversion => 102*tsibclk => 8.5us
  169. * (tsibclk = 1/11981000)
  170. * Period between SIB 128-bit frames = 10.7us
  171. */
  172. /**
  173. * ucb1x00_adc_enable - enable the ADC converter
  174. * @ucb: UCB1x00 structure describing chip
  175. *
  176. * Enable the ucb1x00 and ADC converter on the UCB1x00 for use.
  177. * Any code wishing to use the ADC converter must call this
  178. * function prior to using it.
  179. *
  180. * This function takes the ADC mutex to prevent two or more
  181. * concurrent uses, and therefore may sleep. As a result, it
  182. * can only be called from process context, not interrupt
  183. * context.
  184. *
  185. * You should release the ADC as soon as possible using
  186. * ucb1x00_adc_disable.
  187. */
  188. void ucb1x00_adc_enable(struct ucb1x00 *ucb)
  189. {
  190. mutex_lock(&ucb->adc_mutex);
  191. ucb->adc_cr |= UCB_ADC_ENA;
  192. ucb1x00_enable(ucb);
  193. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  194. }
  195. /**
  196. * ucb1x00_adc_read - read the specified ADC channel
  197. * @ucb: UCB1x00 structure describing chip
  198. * @adc_channel: ADC channel mask
  199. * @sync: wait for syncronisation pulse.
  200. *
  201. * Start an ADC conversion and wait for the result. Note that
  202. * synchronised ADC conversions (via the ADCSYNC pin) must wait
  203. * until the trigger is asserted and the conversion is finished.
  204. *
  205. * This function currently spins waiting for the conversion to
  206. * complete (2 frames max without sync).
  207. *
  208. * If called for a synchronised ADC conversion, it may sleep
  209. * with the ADC mutex held.
  210. */
  211. unsigned int ucb1x00_adc_read(struct ucb1x00 *ucb, int adc_channel, int sync)
  212. {
  213. unsigned int val;
  214. if (sync)
  215. adc_channel |= UCB_ADC_SYNC_ENA;
  216. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel);
  217. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr | adc_channel | UCB_ADC_START);
  218. for (;;) {
  219. val = ucb1x00_reg_read(ucb, UCB_ADC_DATA);
  220. if (val & UCB_ADC_DAT_VAL)
  221. break;
  222. /* yield to other processes */
  223. set_current_state(TASK_INTERRUPTIBLE);
  224. schedule_timeout(1);
  225. }
  226. return UCB_ADC_DAT(val);
  227. }
  228. /**
  229. * ucb1x00_adc_disable - disable the ADC converter
  230. * @ucb: UCB1x00 structure describing chip
  231. *
  232. * Disable the ADC converter and release the ADC mutex.
  233. */
  234. void ucb1x00_adc_disable(struct ucb1x00 *ucb)
  235. {
  236. ucb->adc_cr &= ~UCB_ADC_ENA;
  237. ucb1x00_reg_write(ucb, UCB_ADC_CR, ucb->adc_cr);
  238. ucb1x00_disable(ucb);
  239. mutex_unlock(&ucb->adc_mutex);
  240. }
  241. /*
  242. * UCB1x00 Interrupt handling.
  243. *
  244. * The UCB1x00 can generate interrupts when the SIBCLK is stopped.
  245. * Since we need to read an internal register, we must re-enable
  246. * SIBCLK to talk to the chip. We leave the clock running until
  247. * we have finished processing all interrupts from the chip.
  248. */
  249. static void ucb1x00_irq(unsigned int irq, struct irq_desc *desc)
  250. {
  251. struct ucb1x00 *ucb = irq_desc_get_handler_data(desc);
  252. unsigned int isr, i;
  253. ucb1x00_enable(ucb);
  254. isr = ucb1x00_reg_read(ucb, UCB_IE_STATUS);
  255. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, isr);
  256. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  257. for (i = 0; i < 16 && isr; i++, isr >>= 1, irq++)
  258. if (isr & 1)
  259. generic_handle_irq(ucb->irq_base + i);
  260. ucb1x00_disable(ucb);
  261. }
  262. static void ucb1x00_irq_update(struct ucb1x00 *ucb, unsigned mask)
  263. {
  264. ucb1x00_enable(ucb);
  265. if (ucb->irq_ris_enbl & mask)
  266. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl &
  267. ucb->irq_mask);
  268. if (ucb->irq_fal_enbl & mask)
  269. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl &
  270. ucb->irq_mask);
  271. ucb1x00_disable(ucb);
  272. }
  273. static void ucb1x00_irq_noop(struct irq_data *data)
  274. {
  275. }
  276. static void ucb1x00_irq_mask(struct irq_data *data)
  277. {
  278. struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data);
  279. unsigned mask = 1 << (data->irq - ucb->irq_base);
  280. raw_spin_lock(&ucb->irq_lock);
  281. ucb->irq_mask &= ~mask;
  282. ucb1x00_irq_update(ucb, mask);
  283. raw_spin_unlock(&ucb->irq_lock);
  284. }
  285. static void ucb1x00_irq_unmask(struct irq_data *data)
  286. {
  287. struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data);
  288. unsigned mask = 1 << (data->irq - ucb->irq_base);
  289. raw_spin_lock(&ucb->irq_lock);
  290. ucb->irq_mask |= mask;
  291. ucb1x00_irq_update(ucb, mask);
  292. raw_spin_unlock(&ucb->irq_lock);
  293. }
  294. static int ucb1x00_irq_set_type(struct irq_data *data, unsigned int type)
  295. {
  296. struct ucb1x00 *ucb = irq_data_get_irq_chip_data(data);
  297. unsigned mask = 1 << (data->irq - ucb->irq_base);
  298. raw_spin_lock(&ucb->irq_lock);
  299. if (type & IRQ_TYPE_EDGE_RISING)
  300. ucb->irq_ris_enbl |= mask;
  301. else
  302. ucb->irq_ris_enbl &= ~mask;
  303. if (type & IRQ_TYPE_EDGE_FALLING)
  304. ucb->irq_fal_enbl |= mask;
  305. else
  306. ucb->irq_fal_enbl &= ~mask;
  307. if (ucb->irq_mask & mask) {
  308. ucb1x00_reg_write(ucb, UCB_IE_RIS, ucb->irq_ris_enbl &
  309. ucb->irq_mask);
  310. ucb1x00_reg_write(ucb, UCB_IE_FAL, ucb->irq_fal_enbl &
  311. ucb->irq_mask);
  312. }
  313. raw_spin_unlock(&ucb->irq_lock);
  314. return 0;
  315. }
  316. static struct irq_chip ucb1x00_irqchip = {
  317. .name = "ucb1x00",
  318. .irq_ack = ucb1x00_irq_noop,
  319. .irq_mask = ucb1x00_irq_mask,
  320. .irq_unmask = ucb1x00_irq_unmask,
  321. .irq_set_type = ucb1x00_irq_set_type,
  322. };
  323. static int ucb1x00_add_dev(struct ucb1x00 *ucb, struct ucb1x00_driver *drv)
  324. {
  325. struct ucb1x00_dev *dev;
  326. int ret = -ENOMEM;
  327. dev = kmalloc(sizeof(struct ucb1x00_dev), GFP_KERNEL);
  328. if (dev) {
  329. dev->ucb = ucb;
  330. dev->drv = drv;
  331. ret = drv->add(dev);
  332. if (ret == 0) {
  333. list_add_tail(&dev->dev_node, &ucb->devs);
  334. list_add_tail(&dev->drv_node, &drv->devs);
  335. } else {
  336. kfree(dev);
  337. }
  338. }
  339. return ret;
  340. }
  341. static void ucb1x00_remove_dev(struct ucb1x00_dev *dev)
  342. {
  343. dev->drv->remove(dev);
  344. list_del(&dev->dev_node);
  345. list_del(&dev->drv_node);
  346. kfree(dev);
  347. }
  348. /*
  349. * Try to probe our interrupt, rather than relying on lots of
  350. * hard-coded machine dependencies. For reference, the expected
  351. * IRQ mappings are:
  352. *
  353. * Machine Default IRQ
  354. * adsbitsy IRQ_GPCIN4
  355. * cerf IRQ_GPIO_UCB1200_IRQ
  356. * flexanet IRQ_GPIO_GUI
  357. * freebird IRQ_GPIO_FREEBIRD_UCB1300_IRQ
  358. * graphicsclient ADS_EXT_IRQ(8)
  359. * graphicsmaster ADS_EXT_IRQ(8)
  360. * lart LART_IRQ_UCB1200
  361. * omnimeter IRQ_GPIO23
  362. * pfs168 IRQ_GPIO_UCB1300_IRQ
  363. * simpad IRQ_GPIO_UCB1300_IRQ
  364. * shannon SHANNON_IRQ_GPIO_IRQ_CODEC
  365. * yopy IRQ_GPIO_UCB1200_IRQ
  366. */
  367. static int ucb1x00_detect_irq(struct ucb1x00 *ucb)
  368. {
  369. unsigned long mask;
  370. mask = probe_irq_on();
  371. if (!mask) {
  372. probe_irq_off(mask);
  373. return NO_IRQ;
  374. }
  375. /*
  376. * Enable the ADC interrupt.
  377. */
  378. ucb1x00_reg_write(ucb, UCB_IE_RIS, UCB_IE_ADC);
  379. ucb1x00_reg_write(ucb, UCB_IE_FAL, UCB_IE_ADC);
  380. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  381. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  382. /*
  383. * Cause an ADC interrupt.
  384. */
  385. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA);
  386. ucb1x00_reg_write(ucb, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
  387. /*
  388. * Wait for the conversion to complete.
  389. */
  390. while ((ucb1x00_reg_read(ucb, UCB_ADC_DATA) & UCB_ADC_DAT_VAL) == 0);
  391. ucb1x00_reg_write(ucb, UCB_ADC_CR, 0);
  392. /*
  393. * Disable and clear interrupt.
  394. */
  395. ucb1x00_reg_write(ucb, UCB_IE_RIS, 0);
  396. ucb1x00_reg_write(ucb, UCB_IE_FAL, 0);
  397. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0xffff);
  398. ucb1x00_reg_write(ucb, UCB_IE_CLEAR, 0);
  399. /*
  400. * Read triggered interrupt.
  401. */
  402. return probe_irq_off(mask);
  403. }
  404. static void ucb1x00_release(struct device *dev)
  405. {
  406. struct ucb1x00 *ucb = classdev_to_ucb1x00(dev);
  407. kfree(ucb);
  408. }
  409. static struct class ucb1x00_class = {
  410. .name = "ucb1x00",
  411. .dev_release = ucb1x00_release,
  412. };
  413. static int ucb1x00_probe(struct mcp *mcp)
  414. {
  415. struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data;
  416. struct ucb1x00_driver *drv;
  417. struct ucb1x00 *ucb;
  418. unsigned id, i, irq_base;
  419. int ret = -ENODEV;
  420. /* Tell the platform to deassert the UCB1x00 reset */
  421. if (pdata && pdata->reset)
  422. pdata->reset(UCB_RST_PROBE);
  423. mcp_enable(mcp);
  424. id = mcp_reg_read(mcp, UCB_ID);
  425. mcp_disable(mcp);
  426. if (id != UCB_ID_1200 && id != UCB_ID_1300 && id != UCB_ID_TC35143) {
  427. printk(KERN_WARNING "UCB1x00 ID not found: %04x\n", id);
  428. goto out;
  429. }
  430. ucb = kzalloc(sizeof(struct ucb1x00), GFP_KERNEL);
  431. ret = -ENOMEM;
  432. if (!ucb)
  433. goto out;
  434. device_initialize(&ucb->dev);
  435. ucb->dev.class = &ucb1x00_class;
  436. ucb->dev.parent = &mcp->attached_device;
  437. dev_set_name(&ucb->dev, "ucb1x00");
  438. raw_spin_lock_init(&ucb->irq_lock);
  439. spin_lock_init(&ucb->io_lock);
  440. mutex_init(&ucb->adc_mutex);
  441. ucb->id = id;
  442. ucb->mcp = mcp;
  443. ret = device_add(&ucb->dev);
  444. if (ret)
  445. goto err_dev_add;
  446. ucb1x00_enable(ucb);
  447. ucb->irq = ucb1x00_detect_irq(ucb);
  448. ucb1x00_disable(ucb);
  449. if (ucb->irq == NO_IRQ) {
  450. dev_err(&ucb->dev, "IRQ probe failed\n");
  451. ret = -ENODEV;
  452. goto err_no_irq;
  453. }
  454. ucb->gpio.base = -1;
  455. irq_base = pdata ? pdata->irq_base : 0;
  456. ucb->irq_base = irq_alloc_descs(-1, irq_base, 16, -1);
  457. if (ucb->irq_base < 0) {
  458. dev_err(&ucb->dev, "unable to allocate 16 irqs: %d\n",
  459. ucb->irq_base);
  460. goto err_irq_alloc;
  461. }
  462. for (i = 0; i < 16; i++) {
  463. unsigned irq = ucb->irq_base + i;
  464. irq_set_chip_and_handler(irq, &ucb1x00_irqchip, handle_edge_irq);
  465. irq_set_chip_data(irq, ucb);
  466. set_irq_flags(irq, IRQF_VALID | IRQ_NOREQUEST);
  467. }
  468. irq_set_irq_type(ucb->irq, IRQ_TYPE_EDGE_RISING);
  469. irq_set_handler_data(ucb->irq, ucb);
  470. irq_set_chained_handler(ucb->irq, ucb1x00_irq);
  471. if (pdata && pdata->gpio_base) {
  472. ucb->gpio.label = dev_name(&ucb->dev);
  473. ucb->gpio.dev = &ucb->dev;
  474. ucb->gpio.owner = THIS_MODULE;
  475. ucb->gpio.base = pdata->gpio_base;
  476. ucb->gpio.ngpio = 10;
  477. ucb->gpio.set = ucb1x00_gpio_set;
  478. ucb->gpio.get = ucb1x00_gpio_get;
  479. ucb->gpio.direction_input = ucb1x00_gpio_direction_input;
  480. ucb->gpio.direction_output = ucb1x00_gpio_direction_output;
  481. ucb->gpio.to_irq = ucb1x00_to_irq;
  482. ret = gpiochip_add(&ucb->gpio);
  483. if (ret)
  484. goto err_gpio_add;
  485. } else
  486. dev_info(&ucb->dev, "gpio_base not set so no gpiolib support");
  487. mcp_set_drvdata(mcp, ucb);
  488. INIT_LIST_HEAD(&ucb->devs);
  489. mutex_lock(&ucb1x00_mutex);
  490. list_add_tail(&ucb->node, &ucb1x00_devices);
  491. list_for_each_entry(drv, &ucb1x00_drivers, node) {
  492. ucb1x00_add_dev(ucb, drv);
  493. }
  494. mutex_unlock(&ucb1x00_mutex);
  495. return ret;
  496. err_gpio_add:
  497. irq_set_chained_handler(ucb->irq, NULL);
  498. err_irq_alloc:
  499. if (ucb->irq_base > 0)
  500. irq_free_descs(ucb->irq_base, 16);
  501. err_no_irq:
  502. device_del(&ucb->dev);
  503. err_dev_add:
  504. put_device(&ucb->dev);
  505. out:
  506. if (pdata && pdata->reset)
  507. pdata->reset(UCB_RST_PROBE_FAIL);
  508. return ret;
  509. }
  510. static void ucb1x00_remove(struct mcp *mcp)
  511. {
  512. struct ucb1x00_plat_data *pdata = mcp->attached_device.platform_data;
  513. struct ucb1x00 *ucb = mcp_get_drvdata(mcp);
  514. struct list_head *l, *n;
  515. int ret;
  516. mutex_lock(&ucb1x00_mutex);
  517. list_del(&ucb->node);
  518. list_for_each_safe(l, n, &ucb->devs) {
  519. struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, dev_node);
  520. ucb1x00_remove_dev(dev);
  521. }
  522. mutex_unlock(&ucb1x00_mutex);
  523. if (ucb->gpio.base != -1) {
  524. ret = gpiochip_remove(&ucb->gpio);
  525. if (ret)
  526. dev_err(&ucb->dev, "Can't remove gpio chip: %d\n", ret);
  527. }
  528. irq_set_chained_handler(ucb->irq, NULL);
  529. irq_free_descs(ucb->irq_base, 16);
  530. device_unregister(&ucb->dev);
  531. if (pdata && pdata->reset)
  532. pdata->reset(UCB_RST_REMOVE);
  533. }
  534. int ucb1x00_register_driver(struct ucb1x00_driver *drv)
  535. {
  536. struct ucb1x00 *ucb;
  537. INIT_LIST_HEAD(&drv->devs);
  538. mutex_lock(&ucb1x00_mutex);
  539. list_add_tail(&drv->node, &ucb1x00_drivers);
  540. list_for_each_entry(ucb, &ucb1x00_devices, node) {
  541. ucb1x00_add_dev(ucb, drv);
  542. }
  543. mutex_unlock(&ucb1x00_mutex);
  544. return 0;
  545. }
  546. void ucb1x00_unregister_driver(struct ucb1x00_driver *drv)
  547. {
  548. struct list_head *n, *l;
  549. mutex_lock(&ucb1x00_mutex);
  550. list_del(&drv->node);
  551. list_for_each_safe(l, n, &drv->devs) {
  552. struct ucb1x00_dev *dev = list_entry(l, struct ucb1x00_dev, drv_node);
  553. ucb1x00_remove_dev(dev);
  554. }
  555. mutex_unlock(&ucb1x00_mutex);
  556. }
  557. static int ucb1x00_suspend(struct device *dev)
  558. {
  559. struct ucb1x00 *ucb = dev_get_drvdata(dev);
  560. struct ucb1x00_dev *udev;
  561. mutex_lock(&ucb1x00_mutex);
  562. list_for_each_entry(udev, &ucb->devs, dev_node) {
  563. if (udev->drv->suspend)
  564. udev->drv->suspend(udev);
  565. }
  566. mutex_unlock(&ucb1x00_mutex);
  567. return 0;
  568. }
  569. static int ucb1x00_resume(struct device *dev)
  570. {
  571. struct ucb1x00 *ucb = dev_get_drvdata(dev);
  572. struct ucb1x00_dev *udev;
  573. ucb1x00_enable(ucb);
  574. ucb1x00_reg_write(ucb, UCB_IO_DATA, ucb->io_out);
  575. ucb1x00_reg_write(ucb, UCB_IO_DIR, ucb->io_dir);
  576. ucb1x00_disable(ucb);
  577. mutex_lock(&ucb1x00_mutex);
  578. list_for_each_entry(udev, &ucb->devs, dev_node) {
  579. if (udev->drv->resume)
  580. udev->drv->resume(udev);
  581. }
  582. mutex_unlock(&ucb1x00_mutex);
  583. return 0;
  584. }
  585. static const struct dev_pm_ops ucb1x00_pm_ops = {
  586. SET_SYSTEM_SLEEP_PM_OPS(ucb1x00_suspend, ucb1x00_resume)
  587. };
  588. static struct mcp_driver ucb1x00_driver = {
  589. .drv = {
  590. .name = "ucb1x00",
  591. .owner = THIS_MODULE,
  592. .pm = &ucb1x00_pm_ops,
  593. },
  594. .probe = ucb1x00_probe,
  595. .remove = ucb1x00_remove,
  596. };
  597. static int __init ucb1x00_init(void)
  598. {
  599. int ret = class_register(&ucb1x00_class);
  600. if (ret == 0) {
  601. ret = mcp_driver_register(&ucb1x00_driver);
  602. if (ret)
  603. class_unregister(&ucb1x00_class);
  604. }
  605. return ret;
  606. }
  607. static void __exit ucb1x00_exit(void)
  608. {
  609. mcp_driver_unregister(&ucb1x00_driver);
  610. class_unregister(&ucb1x00_class);
  611. }
  612. module_init(ucb1x00_init);
  613. module_exit(ucb1x00_exit);
  614. EXPORT_SYMBOL(ucb1x00_io_set_dir);
  615. EXPORT_SYMBOL(ucb1x00_io_write);
  616. EXPORT_SYMBOL(ucb1x00_io_read);
  617. EXPORT_SYMBOL(ucb1x00_adc_enable);
  618. EXPORT_SYMBOL(ucb1x00_adc_read);
  619. EXPORT_SYMBOL(ucb1x00_adc_disable);
  620. EXPORT_SYMBOL(ucb1x00_register_driver);
  621. EXPORT_SYMBOL(ucb1x00_unregister_driver);
  622. MODULE_ALIAS("mcp:ucb1x00");
  623. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  624. MODULE_DESCRIPTION("UCB1x00 core driver");
  625. MODULE_LICENSE("GPL");