asic3.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * driver/mfd/asic3.c
  3. *
  4. * Compaq ASIC3 support.
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Copyright 2001 Compaq Computer Corporation.
  11. * Copyright 2004-2005 Phil Blundell
  12. * Copyright 2007-2008 OpenedHand Ltd.
  13. *
  14. * Authors: Phil Blundell <pb@handhelds.org>,
  15. * Samuel Ortiz <sameo@openedhand.com>
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/irq.h>
  21. #include <linux/gpio.h>
  22. #include <linux/export.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mfd/asic3.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/ds1wm.h>
  30. #include <linux/mfd/tmio.h>
  31. enum {
  32. ASIC3_CLOCK_SPI,
  33. ASIC3_CLOCK_OWM,
  34. ASIC3_CLOCK_PWM0,
  35. ASIC3_CLOCK_PWM1,
  36. ASIC3_CLOCK_LED0,
  37. ASIC3_CLOCK_LED1,
  38. ASIC3_CLOCK_LED2,
  39. ASIC3_CLOCK_SD_HOST,
  40. ASIC3_CLOCK_SD_BUS,
  41. ASIC3_CLOCK_SMBUS,
  42. ASIC3_CLOCK_EX0,
  43. ASIC3_CLOCK_EX1,
  44. };
  45. struct asic3_clk {
  46. int enabled;
  47. unsigned int cdex;
  48. unsigned long rate;
  49. };
  50. #define INIT_CDEX(_name, _rate) \
  51. [ASIC3_CLOCK_##_name] = { \
  52. .cdex = CLOCK_CDEX_##_name, \
  53. .rate = _rate, \
  54. }
  55. static struct asic3_clk asic3_clk_init[] __initdata = {
  56. INIT_CDEX(SPI, 0),
  57. INIT_CDEX(OWM, 5000000),
  58. INIT_CDEX(PWM0, 0),
  59. INIT_CDEX(PWM1, 0),
  60. INIT_CDEX(LED0, 0),
  61. INIT_CDEX(LED1, 0),
  62. INIT_CDEX(LED2, 0),
  63. INIT_CDEX(SD_HOST, 24576000),
  64. INIT_CDEX(SD_BUS, 12288000),
  65. INIT_CDEX(SMBUS, 0),
  66. INIT_CDEX(EX0, 32768),
  67. INIT_CDEX(EX1, 24576000),
  68. };
  69. struct asic3 {
  70. void __iomem *mapping;
  71. unsigned int bus_shift;
  72. unsigned int irq_nr;
  73. unsigned int irq_base;
  74. spinlock_t lock;
  75. u16 irq_bothedge[4];
  76. struct gpio_chip gpio;
  77. struct device *dev;
  78. void __iomem *tmio_cnf;
  79. struct asic3_clk clocks[ARRAY_SIZE(asic3_clk_init)];
  80. };
  81. static int asic3_gpio_get(struct gpio_chip *chip, unsigned offset);
  82. void asic3_write_register(struct asic3 *asic, unsigned int reg, u32 value)
  83. {
  84. iowrite16(value, asic->mapping +
  85. (reg >> asic->bus_shift));
  86. }
  87. EXPORT_SYMBOL_GPL(asic3_write_register);
  88. u32 asic3_read_register(struct asic3 *asic, unsigned int reg)
  89. {
  90. return ioread16(asic->mapping +
  91. (reg >> asic->bus_shift));
  92. }
  93. EXPORT_SYMBOL_GPL(asic3_read_register);
  94. static void asic3_set_register(struct asic3 *asic, u32 reg, u32 bits, bool set)
  95. {
  96. unsigned long flags;
  97. u32 val;
  98. spin_lock_irqsave(&asic->lock, flags);
  99. val = asic3_read_register(asic, reg);
  100. if (set)
  101. val |= bits;
  102. else
  103. val &= ~bits;
  104. asic3_write_register(asic, reg, val);
  105. spin_unlock_irqrestore(&asic->lock, flags);
  106. }
  107. /* IRQs */
  108. #define MAX_ASIC_ISR_LOOPS 20
  109. #define ASIC3_GPIO_BASE_INCR \
  110. (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE)
  111. static void asic3_irq_flip_edge(struct asic3 *asic,
  112. u32 base, int bit)
  113. {
  114. u16 edge;
  115. unsigned long flags;
  116. spin_lock_irqsave(&asic->lock, flags);
  117. edge = asic3_read_register(asic,
  118. base + ASIC3_GPIO_EDGE_TRIGGER);
  119. edge ^= bit;
  120. asic3_write_register(asic,
  121. base + ASIC3_GPIO_EDGE_TRIGGER, edge);
  122. spin_unlock_irqrestore(&asic->lock, flags);
  123. }
  124. static void asic3_irq_demux(struct irq_desc *desc)
  125. {
  126. struct asic3 *asic = irq_desc_get_handler_data(desc);
  127. struct irq_data *data = irq_desc_get_irq_data(desc);
  128. int iter, i;
  129. unsigned long flags;
  130. data->chip->irq_ack(data);
  131. for (iter = 0 ; iter < MAX_ASIC_ISR_LOOPS; iter++) {
  132. u32 status;
  133. int bank;
  134. spin_lock_irqsave(&asic->lock, flags);
  135. status = asic3_read_register(asic,
  136. ASIC3_OFFSET(INTR, P_INT_STAT));
  137. spin_unlock_irqrestore(&asic->lock, flags);
  138. /* Check all ten register bits */
  139. if ((status & 0x3ff) == 0)
  140. break;
  141. /* Handle GPIO IRQs */
  142. for (bank = 0; bank < ASIC3_NUM_GPIO_BANKS; bank++) {
  143. if (status & (1 << bank)) {
  144. unsigned long base, istat;
  145. base = ASIC3_GPIO_A_BASE
  146. + bank * ASIC3_GPIO_BASE_INCR;
  147. spin_lock_irqsave(&asic->lock, flags);
  148. istat = asic3_read_register(asic,
  149. base +
  150. ASIC3_GPIO_INT_STATUS);
  151. /* Clearing IntStatus */
  152. asic3_write_register(asic,
  153. base +
  154. ASIC3_GPIO_INT_STATUS, 0);
  155. spin_unlock_irqrestore(&asic->lock, flags);
  156. for (i = 0; i < ASIC3_GPIOS_PER_BANK; i++) {
  157. int bit = (1 << i);
  158. unsigned int irqnr;
  159. if (!(istat & bit))
  160. continue;
  161. irqnr = asic->irq_base +
  162. (ASIC3_GPIOS_PER_BANK * bank)
  163. + i;
  164. generic_handle_irq(irqnr);
  165. if (asic->irq_bothedge[bank] & bit)
  166. asic3_irq_flip_edge(asic, base,
  167. bit);
  168. }
  169. }
  170. }
  171. /* Handle remaining IRQs in the status register */
  172. for (i = ASIC3_NUM_GPIOS; i < ASIC3_NR_IRQS; i++) {
  173. /* They start at bit 4 and go up */
  174. if (status & (1 << (i - ASIC3_NUM_GPIOS + 4)))
  175. generic_handle_irq(asic->irq_base + i);
  176. }
  177. }
  178. if (iter >= MAX_ASIC_ISR_LOOPS)
  179. dev_err(asic->dev, "interrupt processing overrun\n");
  180. }
  181. static inline int asic3_irq_to_bank(struct asic3 *asic, int irq)
  182. {
  183. int n;
  184. n = (irq - asic->irq_base) >> 4;
  185. return (n * (ASIC3_GPIO_B_BASE - ASIC3_GPIO_A_BASE));
  186. }
  187. static inline int asic3_irq_to_index(struct asic3 *asic, int irq)
  188. {
  189. return (irq - asic->irq_base) & 0xf;
  190. }
  191. static void asic3_mask_gpio_irq(struct irq_data *data)
  192. {
  193. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  194. u32 val, bank, index;
  195. unsigned long flags;
  196. bank = asic3_irq_to_bank(asic, data->irq);
  197. index = asic3_irq_to_index(asic, data->irq);
  198. spin_lock_irqsave(&asic->lock, flags);
  199. val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
  200. val |= 1 << index;
  201. asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
  202. spin_unlock_irqrestore(&asic->lock, flags);
  203. }
  204. static void asic3_mask_irq(struct irq_data *data)
  205. {
  206. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  207. int regval;
  208. unsigned long flags;
  209. spin_lock_irqsave(&asic->lock, flags);
  210. regval = asic3_read_register(asic,
  211. ASIC3_INTR_BASE +
  212. ASIC3_INTR_INT_MASK);
  213. regval &= ~(ASIC3_INTMASK_MASK0 <<
  214. (data->irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
  215. asic3_write_register(asic,
  216. ASIC3_INTR_BASE +
  217. ASIC3_INTR_INT_MASK,
  218. regval);
  219. spin_unlock_irqrestore(&asic->lock, flags);
  220. }
  221. static void asic3_unmask_gpio_irq(struct irq_data *data)
  222. {
  223. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  224. u32 val, bank, index;
  225. unsigned long flags;
  226. bank = asic3_irq_to_bank(asic, data->irq);
  227. index = asic3_irq_to_index(asic, data->irq);
  228. spin_lock_irqsave(&asic->lock, flags);
  229. val = asic3_read_register(asic, bank + ASIC3_GPIO_MASK);
  230. val &= ~(1 << index);
  231. asic3_write_register(asic, bank + ASIC3_GPIO_MASK, val);
  232. spin_unlock_irqrestore(&asic->lock, flags);
  233. }
  234. static void asic3_unmask_irq(struct irq_data *data)
  235. {
  236. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  237. int regval;
  238. unsigned long flags;
  239. spin_lock_irqsave(&asic->lock, flags);
  240. regval = asic3_read_register(asic,
  241. ASIC3_INTR_BASE +
  242. ASIC3_INTR_INT_MASK);
  243. regval |= (ASIC3_INTMASK_MASK0 <<
  244. (data->irq - (asic->irq_base + ASIC3_NUM_GPIOS)));
  245. asic3_write_register(asic,
  246. ASIC3_INTR_BASE +
  247. ASIC3_INTR_INT_MASK,
  248. regval);
  249. spin_unlock_irqrestore(&asic->lock, flags);
  250. }
  251. static int asic3_gpio_irq_type(struct irq_data *data, unsigned int type)
  252. {
  253. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  254. u32 bank, index;
  255. u16 trigger, level, edge, bit;
  256. unsigned long flags;
  257. bank = asic3_irq_to_bank(asic, data->irq);
  258. index = asic3_irq_to_index(asic, data->irq);
  259. bit = 1<<index;
  260. spin_lock_irqsave(&asic->lock, flags);
  261. level = asic3_read_register(asic,
  262. bank + ASIC3_GPIO_LEVEL_TRIGGER);
  263. edge = asic3_read_register(asic,
  264. bank + ASIC3_GPIO_EDGE_TRIGGER);
  265. trigger = asic3_read_register(asic,
  266. bank + ASIC3_GPIO_TRIGGER_TYPE);
  267. asic->irq_bothedge[(data->irq - asic->irq_base) >> 4] &= ~bit;
  268. if (type == IRQ_TYPE_EDGE_RISING) {
  269. trigger |= bit;
  270. edge |= bit;
  271. } else if (type == IRQ_TYPE_EDGE_FALLING) {
  272. trigger |= bit;
  273. edge &= ~bit;
  274. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  275. trigger |= bit;
  276. if (asic3_gpio_get(&asic->gpio, data->irq - asic->irq_base))
  277. edge &= ~bit;
  278. else
  279. edge |= bit;
  280. asic->irq_bothedge[(data->irq - asic->irq_base) >> 4] |= bit;
  281. } else if (type == IRQ_TYPE_LEVEL_LOW) {
  282. trigger &= ~bit;
  283. level &= ~bit;
  284. } else if (type == IRQ_TYPE_LEVEL_HIGH) {
  285. trigger &= ~bit;
  286. level |= bit;
  287. } else {
  288. /*
  289. * if type == IRQ_TYPE_NONE, we should mask interrupts, but
  290. * be careful to not unmask them if mask was also called.
  291. * Probably need internal state for mask.
  292. */
  293. dev_notice(asic->dev, "irq type not changed\n");
  294. }
  295. asic3_write_register(asic, bank + ASIC3_GPIO_LEVEL_TRIGGER,
  296. level);
  297. asic3_write_register(asic, bank + ASIC3_GPIO_EDGE_TRIGGER,
  298. edge);
  299. asic3_write_register(asic, bank + ASIC3_GPIO_TRIGGER_TYPE,
  300. trigger);
  301. spin_unlock_irqrestore(&asic->lock, flags);
  302. return 0;
  303. }
  304. static int asic3_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
  305. {
  306. struct asic3 *asic = irq_data_get_irq_chip_data(data);
  307. u32 bank, index;
  308. u16 bit;
  309. bank = asic3_irq_to_bank(asic, data->irq);
  310. index = asic3_irq_to_index(asic, data->irq);
  311. bit = 1<<index;
  312. asic3_set_register(asic, bank + ASIC3_GPIO_SLEEP_MASK, bit, !on);
  313. return 0;
  314. }
  315. static struct irq_chip asic3_gpio_irq_chip = {
  316. .name = "ASIC3-GPIO",
  317. .irq_ack = asic3_mask_gpio_irq,
  318. .irq_mask = asic3_mask_gpio_irq,
  319. .irq_unmask = asic3_unmask_gpio_irq,
  320. .irq_set_type = asic3_gpio_irq_type,
  321. .irq_set_wake = asic3_gpio_irq_set_wake,
  322. };
  323. static struct irq_chip asic3_irq_chip = {
  324. .name = "ASIC3",
  325. .irq_ack = asic3_mask_irq,
  326. .irq_mask = asic3_mask_irq,
  327. .irq_unmask = asic3_unmask_irq,
  328. };
  329. static int __init asic3_irq_probe(struct platform_device *pdev)
  330. {
  331. struct asic3 *asic = platform_get_drvdata(pdev);
  332. unsigned long clksel = 0;
  333. unsigned int irq, irq_base;
  334. int ret;
  335. ret = platform_get_irq(pdev, 0);
  336. if (ret < 0)
  337. return ret;
  338. asic->irq_nr = ret;
  339. /* turn on clock to IRQ controller */
  340. clksel |= CLOCK_SEL_CX;
  341. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
  342. clksel);
  343. irq_base = asic->irq_base;
  344. for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
  345. if (irq < asic->irq_base + ASIC3_NUM_GPIOS)
  346. irq_set_chip(irq, &asic3_gpio_irq_chip);
  347. else
  348. irq_set_chip(irq, &asic3_irq_chip);
  349. irq_set_chip_data(irq, asic);
  350. irq_set_handler(irq, handle_level_irq);
  351. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  352. }
  353. asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
  354. ASIC3_INTMASK_GINTMASK);
  355. irq_set_chained_handler_and_data(asic->irq_nr, asic3_irq_demux, asic);
  356. irq_set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
  357. return 0;
  358. }
  359. static void asic3_irq_remove(struct platform_device *pdev)
  360. {
  361. struct asic3 *asic = platform_get_drvdata(pdev);
  362. unsigned int irq, irq_base;
  363. irq_base = asic->irq_base;
  364. for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
  365. irq_set_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  366. irq_set_chip_and_handler(irq, NULL, NULL);
  367. irq_set_chip_data(irq, NULL);
  368. }
  369. irq_set_chained_handler(asic->irq_nr, NULL);
  370. }
  371. /* GPIOs */
  372. static int asic3_gpio_direction(struct gpio_chip *chip,
  373. unsigned offset, int out)
  374. {
  375. u32 mask = ASIC3_GPIO_TO_MASK(offset), out_reg;
  376. unsigned int gpio_base;
  377. unsigned long flags;
  378. struct asic3 *asic;
  379. asic = container_of(chip, struct asic3, gpio);
  380. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  381. if (gpio_base > ASIC3_GPIO_D_BASE) {
  382. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  383. gpio_base, offset);
  384. return -EINVAL;
  385. }
  386. spin_lock_irqsave(&asic->lock, flags);
  387. out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
  388. /* Input is 0, Output is 1 */
  389. if (out)
  390. out_reg |= mask;
  391. else
  392. out_reg &= ~mask;
  393. asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
  394. spin_unlock_irqrestore(&asic->lock, flags);
  395. return 0;
  396. }
  397. static int asic3_gpio_direction_input(struct gpio_chip *chip,
  398. unsigned offset)
  399. {
  400. return asic3_gpio_direction(chip, offset, 0);
  401. }
  402. static int asic3_gpio_direction_output(struct gpio_chip *chip,
  403. unsigned offset, int value)
  404. {
  405. return asic3_gpio_direction(chip, offset, 1);
  406. }
  407. static int asic3_gpio_get(struct gpio_chip *chip,
  408. unsigned offset)
  409. {
  410. unsigned int gpio_base;
  411. u32 mask = ASIC3_GPIO_TO_MASK(offset);
  412. struct asic3 *asic;
  413. asic = container_of(chip, struct asic3, gpio);
  414. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  415. if (gpio_base > ASIC3_GPIO_D_BASE) {
  416. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  417. gpio_base, offset);
  418. return -EINVAL;
  419. }
  420. return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
  421. }
  422. static void asic3_gpio_set(struct gpio_chip *chip,
  423. unsigned offset, int value)
  424. {
  425. u32 mask, out_reg;
  426. unsigned int gpio_base;
  427. unsigned long flags;
  428. struct asic3 *asic;
  429. asic = container_of(chip, struct asic3, gpio);
  430. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  431. if (gpio_base > ASIC3_GPIO_D_BASE) {
  432. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  433. gpio_base, offset);
  434. return;
  435. }
  436. mask = ASIC3_GPIO_TO_MASK(offset);
  437. spin_lock_irqsave(&asic->lock, flags);
  438. out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
  439. if (value)
  440. out_reg |= mask;
  441. else
  442. out_reg &= ~mask;
  443. asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
  444. spin_unlock_irqrestore(&asic->lock, flags);
  445. return;
  446. }
  447. static int asic3_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  448. {
  449. struct asic3 *asic = container_of(chip, struct asic3, gpio);
  450. return asic->irq_base + offset;
  451. }
  452. static __init int asic3_gpio_probe(struct platform_device *pdev,
  453. u16 *gpio_config, int num)
  454. {
  455. struct asic3 *asic = platform_get_drvdata(pdev);
  456. u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
  457. u16 out_reg[ASIC3_NUM_GPIO_BANKS];
  458. u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
  459. int i;
  460. memset(alt_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  461. memset(out_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  462. memset(dir_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  463. /* Enable all GPIOs */
  464. asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, MASK), 0xffff);
  465. asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, MASK), 0xffff);
  466. asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, MASK), 0xffff);
  467. asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, MASK), 0xffff);
  468. for (i = 0; i < num; i++) {
  469. u8 alt, pin, dir, init, bank_num, bit_num;
  470. u16 config = gpio_config[i];
  471. pin = ASIC3_CONFIG_GPIO_PIN(config);
  472. alt = ASIC3_CONFIG_GPIO_ALT(config);
  473. dir = ASIC3_CONFIG_GPIO_DIR(config);
  474. init = ASIC3_CONFIG_GPIO_INIT(config);
  475. bank_num = ASIC3_GPIO_TO_BANK(pin);
  476. bit_num = ASIC3_GPIO_TO_BIT(pin);
  477. alt_reg[bank_num] |= (alt << bit_num);
  478. out_reg[bank_num] |= (init << bit_num);
  479. dir_reg[bank_num] |= (dir << bit_num);
  480. }
  481. for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
  482. asic3_write_register(asic,
  483. ASIC3_BANK_TO_BASE(i) +
  484. ASIC3_GPIO_DIRECTION,
  485. dir_reg[i]);
  486. asic3_write_register(asic,
  487. ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
  488. out_reg[i]);
  489. asic3_write_register(asic,
  490. ASIC3_BANK_TO_BASE(i) +
  491. ASIC3_GPIO_ALT_FUNCTION,
  492. alt_reg[i]);
  493. }
  494. return gpiochip_add(&asic->gpio);
  495. }
  496. static int asic3_gpio_remove(struct platform_device *pdev)
  497. {
  498. struct asic3 *asic = platform_get_drvdata(pdev);
  499. gpiochip_remove(&asic->gpio);
  500. return 0;
  501. }
  502. static void asic3_clk_enable(struct asic3 *asic, struct asic3_clk *clk)
  503. {
  504. unsigned long flags;
  505. u32 cdex;
  506. spin_lock_irqsave(&asic->lock, flags);
  507. if (clk->enabled++ == 0) {
  508. cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
  509. cdex |= clk->cdex;
  510. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
  511. }
  512. spin_unlock_irqrestore(&asic->lock, flags);
  513. }
  514. static void asic3_clk_disable(struct asic3 *asic, struct asic3_clk *clk)
  515. {
  516. unsigned long flags;
  517. u32 cdex;
  518. WARN_ON(clk->enabled == 0);
  519. spin_lock_irqsave(&asic->lock, flags);
  520. if (--clk->enabled == 0) {
  521. cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
  522. cdex &= ~clk->cdex;
  523. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
  524. }
  525. spin_unlock_irqrestore(&asic->lock, flags);
  526. }
  527. /* MFD cells (SPI, PWM, LED, DS1WM, MMC) */
  528. static struct ds1wm_driver_data ds1wm_pdata = {
  529. .active_high = 1,
  530. .reset_recover_delay = 1,
  531. };
  532. static struct resource ds1wm_resources[] = {
  533. {
  534. .start = ASIC3_OWM_BASE,
  535. .end = ASIC3_OWM_BASE + 0x13,
  536. .flags = IORESOURCE_MEM,
  537. },
  538. {
  539. .start = ASIC3_IRQ_OWM,
  540. .end = ASIC3_IRQ_OWM,
  541. .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
  542. },
  543. };
  544. static int ds1wm_enable(struct platform_device *pdev)
  545. {
  546. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  547. /* Turn on external clocks and the OWM clock */
  548. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  549. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  550. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
  551. msleep(1);
  552. /* Reset and enable DS1WM */
  553. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  554. ASIC3_EXTCF_OWM_RESET, 1);
  555. msleep(1);
  556. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  557. ASIC3_EXTCF_OWM_RESET, 0);
  558. msleep(1);
  559. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  560. ASIC3_EXTCF_OWM_EN, 1);
  561. msleep(1);
  562. return 0;
  563. }
  564. static int ds1wm_disable(struct platform_device *pdev)
  565. {
  566. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  567. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  568. ASIC3_EXTCF_OWM_EN, 0);
  569. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
  570. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  571. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  572. return 0;
  573. }
  574. static const struct mfd_cell asic3_cell_ds1wm = {
  575. .name = "ds1wm",
  576. .enable = ds1wm_enable,
  577. .disable = ds1wm_disable,
  578. .platform_data = &ds1wm_pdata,
  579. .pdata_size = sizeof(ds1wm_pdata),
  580. .num_resources = ARRAY_SIZE(ds1wm_resources),
  581. .resources = ds1wm_resources,
  582. };
  583. static void asic3_mmc_pwr(struct platform_device *pdev, int state)
  584. {
  585. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  586. tmio_core_mmc_pwr(asic->tmio_cnf, 1 - asic->bus_shift, state);
  587. }
  588. static void asic3_mmc_clk_div(struct platform_device *pdev, int state)
  589. {
  590. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  591. tmio_core_mmc_clk_div(asic->tmio_cnf, 1 - asic->bus_shift, state);
  592. }
  593. static struct tmio_mmc_data asic3_mmc_data = {
  594. .hclk = 24576000,
  595. .set_pwr = asic3_mmc_pwr,
  596. .set_clk_div = asic3_mmc_clk_div,
  597. };
  598. static struct resource asic3_mmc_resources[] = {
  599. {
  600. .start = ASIC3_SD_CTRL_BASE,
  601. .end = ASIC3_SD_CTRL_BASE + 0x3ff,
  602. .flags = IORESOURCE_MEM,
  603. },
  604. {
  605. .start = 0,
  606. .end = 0,
  607. .flags = IORESOURCE_IRQ,
  608. },
  609. };
  610. static int asic3_mmc_enable(struct platform_device *pdev)
  611. {
  612. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  613. /* Not sure if it must be done bit by bit, but leaving as-is */
  614. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  615. ASIC3_SDHWCTRL_LEVCD, 1);
  616. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  617. ASIC3_SDHWCTRL_LEVWP, 1);
  618. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  619. ASIC3_SDHWCTRL_SUSPEND, 0);
  620. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  621. ASIC3_SDHWCTRL_PCLR, 0);
  622. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  623. /* CLK32 used for card detection and for interruption detection
  624. * when HCLK is stopped.
  625. */
  626. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  627. msleep(1);
  628. /* HCLK 24.576 MHz, BCLK 12.288 MHz: */
  629. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
  630. CLOCK_SEL_CX | CLOCK_SEL_SD_HCLK_SEL);
  631. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
  632. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
  633. msleep(1);
  634. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  635. ASIC3_EXTCF_SD_MEM_ENABLE, 1);
  636. /* Enable SD card slot 3.3V power supply */
  637. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  638. ASIC3_SDHWCTRL_SDPWR, 1);
  639. /* ASIC3_SD_CTRL_BASE assumes 32-bit addressing, TMIO is 16-bit */
  640. tmio_core_mmc_enable(asic->tmio_cnf, 1 - asic->bus_shift,
  641. ASIC3_SD_CTRL_BASE >> 1);
  642. return 0;
  643. }
  644. static int asic3_mmc_disable(struct platform_device *pdev)
  645. {
  646. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  647. /* Put in suspend mode */
  648. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  649. ASIC3_SDHWCTRL_SUSPEND, 1);
  650. /* Disable clocks */
  651. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
  652. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
  653. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  654. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  655. return 0;
  656. }
  657. static const struct mfd_cell asic3_cell_mmc = {
  658. .name = "tmio-mmc",
  659. .enable = asic3_mmc_enable,
  660. .disable = asic3_mmc_disable,
  661. .suspend = asic3_mmc_disable,
  662. .resume = asic3_mmc_enable,
  663. .platform_data = &asic3_mmc_data,
  664. .pdata_size = sizeof(asic3_mmc_data),
  665. .num_resources = ARRAY_SIZE(asic3_mmc_resources),
  666. .resources = asic3_mmc_resources,
  667. };
  668. static const int clock_ledn[ASIC3_NUM_LEDS] = {
  669. [0] = ASIC3_CLOCK_LED0,
  670. [1] = ASIC3_CLOCK_LED1,
  671. [2] = ASIC3_CLOCK_LED2,
  672. };
  673. static int asic3_leds_enable(struct platform_device *pdev)
  674. {
  675. const struct mfd_cell *cell = mfd_get_cell(pdev);
  676. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  677. asic3_clk_enable(asic, &asic->clocks[clock_ledn[cell->id]]);
  678. return 0;
  679. }
  680. static int asic3_leds_disable(struct platform_device *pdev)
  681. {
  682. const struct mfd_cell *cell = mfd_get_cell(pdev);
  683. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  684. asic3_clk_disable(asic, &asic->clocks[clock_ledn[cell->id]]);
  685. return 0;
  686. }
  687. static int asic3_leds_suspend(struct platform_device *pdev)
  688. {
  689. const struct mfd_cell *cell = mfd_get_cell(pdev);
  690. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  691. while (asic3_gpio_get(&asic->gpio, ASIC3_GPIO(C, cell->id)) != 0)
  692. msleep(1);
  693. asic3_clk_disable(asic, &asic->clocks[clock_ledn[cell->id]]);
  694. return 0;
  695. }
  696. static struct mfd_cell asic3_cell_leds[ASIC3_NUM_LEDS] = {
  697. [0] = {
  698. .name = "leds-asic3",
  699. .id = 0,
  700. .enable = asic3_leds_enable,
  701. .disable = asic3_leds_disable,
  702. .suspend = asic3_leds_suspend,
  703. .resume = asic3_leds_enable,
  704. },
  705. [1] = {
  706. .name = "leds-asic3",
  707. .id = 1,
  708. .enable = asic3_leds_enable,
  709. .disable = asic3_leds_disable,
  710. .suspend = asic3_leds_suspend,
  711. .resume = asic3_leds_enable,
  712. },
  713. [2] = {
  714. .name = "leds-asic3",
  715. .id = 2,
  716. .enable = asic3_leds_enable,
  717. .disable = asic3_leds_disable,
  718. .suspend = asic3_leds_suspend,
  719. .resume = asic3_leds_enable,
  720. },
  721. };
  722. static int __init asic3_mfd_probe(struct platform_device *pdev,
  723. struct asic3_platform_data *pdata,
  724. struct resource *mem)
  725. {
  726. struct asic3 *asic = platform_get_drvdata(pdev);
  727. struct resource *mem_sdio;
  728. int irq, ret;
  729. mem_sdio = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  730. if (!mem_sdio)
  731. dev_dbg(asic->dev, "no SDIO MEM resource\n");
  732. irq = platform_get_irq(pdev, 1);
  733. if (irq < 0)
  734. dev_dbg(asic->dev, "no SDIO IRQ resource\n");
  735. /* DS1WM */
  736. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  737. ASIC3_EXTCF_OWM_SMB, 0);
  738. ds1wm_resources[0].start >>= asic->bus_shift;
  739. ds1wm_resources[0].end >>= asic->bus_shift;
  740. /* MMC */
  741. if (mem_sdio) {
  742. asic->tmio_cnf = ioremap((ASIC3_SD_CONFIG_BASE >> asic->bus_shift) +
  743. mem_sdio->start,
  744. ASIC3_SD_CONFIG_SIZE >> asic->bus_shift);
  745. if (!asic->tmio_cnf) {
  746. ret = -ENOMEM;
  747. dev_dbg(asic->dev, "Couldn't ioremap SD_CONFIG\n");
  748. goto out;
  749. }
  750. }
  751. asic3_mmc_resources[0].start >>= asic->bus_shift;
  752. asic3_mmc_resources[0].end >>= asic->bus_shift;
  753. if (pdata->clock_rate) {
  754. ds1wm_pdata.clock_rate = pdata->clock_rate;
  755. ret = mfd_add_devices(&pdev->dev, pdev->id,
  756. &asic3_cell_ds1wm, 1, mem, asic->irq_base, NULL);
  757. if (ret < 0)
  758. goto out;
  759. }
  760. if (mem_sdio && (irq >= 0)) {
  761. ret = mfd_add_devices(&pdev->dev, pdev->id,
  762. &asic3_cell_mmc, 1, mem_sdio, irq, NULL);
  763. if (ret < 0)
  764. goto out;
  765. }
  766. ret = 0;
  767. if (pdata->leds) {
  768. int i;
  769. for (i = 0; i < ASIC3_NUM_LEDS; ++i) {
  770. asic3_cell_leds[i].platform_data = &pdata->leds[i];
  771. asic3_cell_leds[i].pdata_size = sizeof(pdata->leds[i]);
  772. }
  773. ret = mfd_add_devices(&pdev->dev, 0,
  774. asic3_cell_leds, ASIC3_NUM_LEDS, NULL, 0, NULL);
  775. }
  776. out:
  777. return ret;
  778. }
  779. static void asic3_mfd_remove(struct platform_device *pdev)
  780. {
  781. struct asic3 *asic = platform_get_drvdata(pdev);
  782. mfd_remove_devices(&pdev->dev);
  783. iounmap(asic->tmio_cnf);
  784. }
  785. /* Core */
  786. static int __init asic3_probe(struct platform_device *pdev)
  787. {
  788. struct asic3_platform_data *pdata = dev_get_platdata(&pdev->dev);
  789. struct asic3 *asic;
  790. struct resource *mem;
  791. unsigned long clksel;
  792. int ret = 0;
  793. asic = devm_kzalloc(&pdev->dev,
  794. sizeof(struct asic3), GFP_KERNEL);
  795. if (asic == NULL) {
  796. printk(KERN_ERR "kzalloc failed\n");
  797. return -ENOMEM;
  798. }
  799. spin_lock_init(&asic->lock);
  800. platform_set_drvdata(pdev, asic);
  801. asic->dev = &pdev->dev;
  802. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  803. if (!mem) {
  804. dev_err(asic->dev, "no MEM resource\n");
  805. return -ENOMEM;
  806. }
  807. asic->mapping = ioremap(mem->start, resource_size(mem));
  808. if (!asic->mapping) {
  809. dev_err(asic->dev, "Couldn't ioremap\n");
  810. return -ENOMEM;
  811. }
  812. asic->irq_base = pdata->irq_base;
  813. /* calculate bus shift from mem resource */
  814. asic->bus_shift = 2 - (resource_size(mem) >> 12);
  815. clksel = 0;
  816. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);
  817. ret = asic3_irq_probe(pdev);
  818. if (ret < 0) {
  819. dev_err(asic->dev, "Couldn't probe IRQs\n");
  820. goto out_unmap;
  821. }
  822. asic->gpio.label = "asic3";
  823. asic->gpio.base = pdata->gpio_base;
  824. asic->gpio.ngpio = ASIC3_NUM_GPIOS;
  825. asic->gpio.get = asic3_gpio_get;
  826. asic->gpio.set = asic3_gpio_set;
  827. asic->gpio.direction_input = asic3_gpio_direction_input;
  828. asic->gpio.direction_output = asic3_gpio_direction_output;
  829. asic->gpio.to_irq = asic3_gpio_to_irq;
  830. ret = asic3_gpio_probe(pdev,
  831. pdata->gpio_config,
  832. pdata->gpio_config_num);
  833. if (ret < 0) {
  834. dev_err(asic->dev, "GPIO probe failed\n");
  835. goto out_irq;
  836. }
  837. /* Making a per-device copy is only needed for the
  838. * theoretical case of multiple ASIC3s on one board:
  839. */
  840. memcpy(asic->clocks, asic3_clk_init, sizeof(asic3_clk_init));
  841. asic3_mfd_probe(pdev, pdata, mem);
  842. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  843. (ASIC3_EXTCF_CF0_BUF_EN|ASIC3_EXTCF_CF0_PWAIT_EN), 1);
  844. dev_info(asic->dev, "ASIC3 Core driver\n");
  845. return 0;
  846. out_irq:
  847. asic3_irq_remove(pdev);
  848. out_unmap:
  849. iounmap(asic->mapping);
  850. return ret;
  851. }
  852. static int asic3_remove(struct platform_device *pdev)
  853. {
  854. int ret;
  855. struct asic3 *asic = platform_get_drvdata(pdev);
  856. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  857. (ASIC3_EXTCF_CF0_BUF_EN|ASIC3_EXTCF_CF0_PWAIT_EN), 0);
  858. asic3_mfd_remove(pdev);
  859. ret = asic3_gpio_remove(pdev);
  860. if (ret < 0)
  861. return ret;
  862. asic3_irq_remove(pdev);
  863. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);
  864. iounmap(asic->mapping);
  865. return 0;
  866. }
  867. static void asic3_shutdown(struct platform_device *pdev)
  868. {
  869. }
  870. static struct platform_driver asic3_device_driver = {
  871. .driver = {
  872. .name = "asic3",
  873. },
  874. .remove = asic3_remove,
  875. .shutdown = asic3_shutdown,
  876. };
  877. static int __init asic3_init(void)
  878. {
  879. int retval = 0;
  880. retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
  881. return retval;
  882. }
  883. subsys_initcall(asic3_init);