asic3.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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(unsigned int irq, 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 struct irq_chip asic3_gpio_irq_chip = {
  305. .name = "ASIC3-GPIO",
  306. .irq_ack = asic3_mask_gpio_irq,
  307. .irq_mask = asic3_mask_gpio_irq,
  308. .irq_unmask = asic3_unmask_gpio_irq,
  309. .irq_set_type = asic3_gpio_irq_type,
  310. };
  311. static struct irq_chip asic3_irq_chip = {
  312. .name = "ASIC3",
  313. .irq_ack = asic3_mask_irq,
  314. .irq_mask = asic3_mask_irq,
  315. .irq_unmask = asic3_unmask_irq,
  316. };
  317. static int __init asic3_irq_probe(struct platform_device *pdev)
  318. {
  319. struct asic3 *asic = platform_get_drvdata(pdev);
  320. unsigned long clksel = 0;
  321. unsigned int irq, irq_base;
  322. int ret;
  323. ret = platform_get_irq(pdev, 0);
  324. if (ret < 0)
  325. return ret;
  326. asic->irq_nr = ret;
  327. /* turn on clock to IRQ controller */
  328. clksel |= CLOCK_SEL_CX;
  329. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
  330. clksel);
  331. irq_base = asic->irq_base;
  332. for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
  333. if (irq < asic->irq_base + ASIC3_NUM_GPIOS)
  334. irq_set_chip(irq, &asic3_gpio_irq_chip);
  335. else
  336. irq_set_chip(irq, &asic3_irq_chip);
  337. irq_set_chip_data(irq, asic);
  338. irq_set_handler(irq, handle_level_irq);
  339. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  340. }
  341. asic3_write_register(asic, ASIC3_OFFSET(INTR, INT_MASK),
  342. ASIC3_INTMASK_GINTMASK);
  343. irq_set_chained_handler(asic->irq_nr, asic3_irq_demux);
  344. irq_set_irq_type(asic->irq_nr, IRQ_TYPE_EDGE_RISING);
  345. irq_set_handler_data(asic->irq_nr, asic);
  346. return 0;
  347. }
  348. static void asic3_irq_remove(struct platform_device *pdev)
  349. {
  350. struct asic3 *asic = platform_get_drvdata(pdev);
  351. unsigned int irq, irq_base;
  352. irq_base = asic->irq_base;
  353. for (irq = irq_base; irq < irq_base + ASIC3_NR_IRQS; irq++) {
  354. set_irq_flags(irq, 0);
  355. irq_set_chip_and_handler(irq, NULL, NULL);
  356. irq_set_chip_data(irq, NULL);
  357. }
  358. irq_set_chained_handler(asic->irq_nr, NULL);
  359. }
  360. /* GPIOs */
  361. static int asic3_gpio_direction(struct gpio_chip *chip,
  362. unsigned offset, int out)
  363. {
  364. u32 mask = ASIC3_GPIO_TO_MASK(offset), out_reg;
  365. unsigned int gpio_base;
  366. unsigned long flags;
  367. struct asic3 *asic;
  368. asic = container_of(chip, struct asic3, gpio);
  369. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  370. if (gpio_base > ASIC3_GPIO_D_BASE) {
  371. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  372. gpio_base, offset);
  373. return -EINVAL;
  374. }
  375. spin_lock_irqsave(&asic->lock, flags);
  376. out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_DIRECTION);
  377. /* Input is 0, Output is 1 */
  378. if (out)
  379. out_reg |= mask;
  380. else
  381. out_reg &= ~mask;
  382. asic3_write_register(asic, gpio_base + ASIC3_GPIO_DIRECTION, out_reg);
  383. spin_unlock_irqrestore(&asic->lock, flags);
  384. return 0;
  385. }
  386. static int asic3_gpio_direction_input(struct gpio_chip *chip,
  387. unsigned offset)
  388. {
  389. return asic3_gpio_direction(chip, offset, 0);
  390. }
  391. static int asic3_gpio_direction_output(struct gpio_chip *chip,
  392. unsigned offset, int value)
  393. {
  394. return asic3_gpio_direction(chip, offset, 1);
  395. }
  396. static int asic3_gpio_get(struct gpio_chip *chip,
  397. unsigned offset)
  398. {
  399. unsigned int gpio_base;
  400. u32 mask = ASIC3_GPIO_TO_MASK(offset);
  401. struct asic3 *asic;
  402. asic = container_of(chip, struct asic3, gpio);
  403. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  404. if (gpio_base > ASIC3_GPIO_D_BASE) {
  405. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  406. gpio_base, offset);
  407. return -EINVAL;
  408. }
  409. return asic3_read_register(asic, gpio_base + ASIC3_GPIO_STATUS) & mask;
  410. }
  411. static void asic3_gpio_set(struct gpio_chip *chip,
  412. unsigned offset, int value)
  413. {
  414. u32 mask, out_reg;
  415. unsigned int gpio_base;
  416. unsigned long flags;
  417. struct asic3 *asic;
  418. asic = container_of(chip, struct asic3, gpio);
  419. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  420. if (gpio_base > ASIC3_GPIO_D_BASE) {
  421. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  422. gpio_base, offset);
  423. return;
  424. }
  425. mask = ASIC3_GPIO_TO_MASK(offset);
  426. spin_lock_irqsave(&asic->lock, flags);
  427. out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
  428. if (value)
  429. out_reg |= mask;
  430. else
  431. out_reg &= ~mask;
  432. asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
  433. spin_unlock_irqrestore(&asic->lock, flags);
  434. return;
  435. }
  436. static int asic3_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  437. {
  438. struct asic3 *asic = container_of(chip, struct asic3, gpio);
  439. return (offset < ASIC3_NUM_GPIOS) ? asic->irq_base + offset : -ENXIO;
  440. }
  441. static __init int asic3_gpio_probe(struct platform_device *pdev,
  442. u16 *gpio_config, int num)
  443. {
  444. struct asic3 *asic = platform_get_drvdata(pdev);
  445. u16 alt_reg[ASIC3_NUM_GPIO_BANKS];
  446. u16 out_reg[ASIC3_NUM_GPIO_BANKS];
  447. u16 dir_reg[ASIC3_NUM_GPIO_BANKS];
  448. int i;
  449. memset(alt_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  450. memset(out_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  451. memset(dir_reg, 0, ASIC3_NUM_GPIO_BANKS * sizeof(u16));
  452. /* Enable all GPIOs */
  453. asic3_write_register(asic, ASIC3_GPIO_OFFSET(A, MASK), 0xffff);
  454. asic3_write_register(asic, ASIC3_GPIO_OFFSET(B, MASK), 0xffff);
  455. asic3_write_register(asic, ASIC3_GPIO_OFFSET(C, MASK), 0xffff);
  456. asic3_write_register(asic, ASIC3_GPIO_OFFSET(D, MASK), 0xffff);
  457. for (i = 0; i < num; i++) {
  458. u8 alt, pin, dir, init, bank_num, bit_num;
  459. u16 config = gpio_config[i];
  460. pin = ASIC3_CONFIG_GPIO_PIN(config);
  461. alt = ASIC3_CONFIG_GPIO_ALT(config);
  462. dir = ASIC3_CONFIG_GPIO_DIR(config);
  463. init = ASIC3_CONFIG_GPIO_INIT(config);
  464. bank_num = ASIC3_GPIO_TO_BANK(pin);
  465. bit_num = ASIC3_GPIO_TO_BIT(pin);
  466. alt_reg[bank_num] |= (alt << bit_num);
  467. out_reg[bank_num] |= (init << bit_num);
  468. dir_reg[bank_num] |= (dir << bit_num);
  469. }
  470. for (i = 0; i < ASIC3_NUM_GPIO_BANKS; i++) {
  471. asic3_write_register(asic,
  472. ASIC3_BANK_TO_BASE(i) +
  473. ASIC3_GPIO_DIRECTION,
  474. dir_reg[i]);
  475. asic3_write_register(asic,
  476. ASIC3_BANK_TO_BASE(i) + ASIC3_GPIO_OUT,
  477. out_reg[i]);
  478. asic3_write_register(asic,
  479. ASIC3_BANK_TO_BASE(i) +
  480. ASIC3_GPIO_ALT_FUNCTION,
  481. alt_reg[i]);
  482. }
  483. return gpiochip_add(&asic->gpio);
  484. }
  485. static int asic3_gpio_remove(struct platform_device *pdev)
  486. {
  487. struct asic3 *asic = platform_get_drvdata(pdev);
  488. return gpiochip_remove(&asic->gpio);
  489. }
  490. static void asic3_clk_enable(struct asic3 *asic, struct asic3_clk *clk)
  491. {
  492. unsigned long flags;
  493. u32 cdex;
  494. spin_lock_irqsave(&asic->lock, flags);
  495. if (clk->enabled++ == 0) {
  496. cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
  497. cdex |= clk->cdex;
  498. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
  499. }
  500. spin_unlock_irqrestore(&asic->lock, flags);
  501. }
  502. static void asic3_clk_disable(struct asic3 *asic, struct asic3_clk *clk)
  503. {
  504. unsigned long flags;
  505. u32 cdex;
  506. WARN_ON(clk->enabled == 0);
  507. spin_lock_irqsave(&asic->lock, flags);
  508. if (--clk->enabled == 0) {
  509. cdex = asic3_read_register(asic, ASIC3_OFFSET(CLOCK, CDEX));
  510. cdex &= ~clk->cdex;
  511. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, CDEX), cdex);
  512. }
  513. spin_unlock_irqrestore(&asic->lock, flags);
  514. }
  515. /* MFD cells (SPI, PWM, LED, DS1WM, MMC) */
  516. static struct ds1wm_driver_data ds1wm_pdata = {
  517. .active_high = 1,
  518. .reset_recover_delay = 1,
  519. };
  520. static struct resource ds1wm_resources[] = {
  521. {
  522. .start = ASIC3_OWM_BASE,
  523. .end = ASIC3_OWM_BASE + 0x13,
  524. .flags = IORESOURCE_MEM,
  525. },
  526. {
  527. .start = ASIC3_IRQ_OWM,
  528. .end = ASIC3_IRQ_OWM,
  529. .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE,
  530. },
  531. };
  532. static int ds1wm_enable(struct platform_device *pdev)
  533. {
  534. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  535. /* Turn on external clocks and the OWM clock */
  536. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  537. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  538. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
  539. msleep(1);
  540. /* Reset and enable DS1WM */
  541. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  542. ASIC3_EXTCF_OWM_RESET, 1);
  543. msleep(1);
  544. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  545. ASIC3_EXTCF_OWM_RESET, 0);
  546. msleep(1);
  547. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  548. ASIC3_EXTCF_OWM_EN, 1);
  549. msleep(1);
  550. return 0;
  551. }
  552. static int ds1wm_disable(struct platform_device *pdev)
  553. {
  554. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  555. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  556. ASIC3_EXTCF_OWM_EN, 0);
  557. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_OWM]);
  558. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  559. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  560. return 0;
  561. }
  562. static struct mfd_cell asic3_cell_ds1wm = {
  563. .name = "ds1wm",
  564. .enable = ds1wm_enable,
  565. .disable = ds1wm_disable,
  566. .platform_data = &ds1wm_pdata,
  567. .pdata_size = sizeof(ds1wm_pdata),
  568. .num_resources = ARRAY_SIZE(ds1wm_resources),
  569. .resources = ds1wm_resources,
  570. };
  571. static void asic3_mmc_pwr(struct platform_device *pdev, int state)
  572. {
  573. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  574. tmio_core_mmc_pwr(asic->tmio_cnf, 1 - asic->bus_shift, state);
  575. }
  576. static void asic3_mmc_clk_div(struct platform_device *pdev, int state)
  577. {
  578. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  579. tmio_core_mmc_clk_div(asic->tmio_cnf, 1 - asic->bus_shift, state);
  580. }
  581. static struct tmio_mmc_data asic3_mmc_data = {
  582. .hclk = 24576000,
  583. .set_pwr = asic3_mmc_pwr,
  584. .set_clk_div = asic3_mmc_clk_div,
  585. };
  586. static struct resource asic3_mmc_resources[] = {
  587. {
  588. .start = ASIC3_SD_CTRL_BASE,
  589. .end = ASIC3_SD_CTRL_BASE + 0x3ff,
  590. .flags = IORESOURCE_MEM,
  591. },
  592. {
  593. .start = 0,
  594. .end = 0,
  595. .flags = IORESOURCE_IRQ,
  596. },
  597. };
  598. static int asic3_mmc_enable(struct platform_device *pdev)
  599. {
  600. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  601. /* Not sure if it must be done bit by bit, but leaving as-is */
  602. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  603. ASIC3_SDHWCTRL_LEVCD, 1);
  604. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  605. ASIC3_SDHWCTRL_LEVWP, 1);
  606. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  607. ASIC3_SDHWCTRL_SUSPEND, 0);
  608. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  609. ASIC3_SDHWCTRL_PCLR, 0);
  610. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  611. /* CLK32 used for card detection and for interruption detection
  612. * when HCLK is stopped.
  613. */
  614. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  615. msleep(1);
  616. /* HCLK 24.576 MHz, BCLK 12.288 MHz: */
  617. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL),
  618. CLOCK_SEL_CX | CLOCK_SEL_SD_HCLK_SEL);
  619. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
  620. asic3_clk_enable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
  621. msleep(1);
  622. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  623. ASIC3_EXTCF_SD_MEM_ENABLE, 1);
  624. /* Enable SD card slot 3.3V power supply */
  625. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  626. ASIC3_SDHWCTRL_SDPWR, 1);
  627. /* ASIC3_SD_CTRL_BASE assumes 32-bit addressing, TMIO is 16-bit */
  628. tmio_core_mmc_enable(asic->tmio_cnf, 1 - asic->bus_shift,
  629. ASIC3_SD_CTRL_BASE >> 1);
  630. return 0;
  631. }
  632. static int asic3_mmc_disable(struct platform_device *pdev)
  633. {
  634. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  635. /* Put in suspend mode */
  636. asic3_set_register(asic, ASIC3_OFFSET(SDHWCTRL, SDCONF),
  637. ASIC3_SDHWCTRL_SUSPEND, 1);
  638. /* Disable clocks */
  639. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_HOST]);
  640. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_SD_BUS]);
  641. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX0]);
  642. asic3_clk_disable(asic, &asic->clocks[ASIC3_CLOCK_EX1]);
  643. return 0;
  644. }
  645. static struct mfd_cell asic3_cell_mmc = {
  646. .name = "tmio-mmc",
  647. .enable = asic3_mmc_enable,
  648. .disable = asic3_mmc_disable,
  649. .suspend = asic3_mmc_disable,
  650. .resume = asic3_mmc_enable,
  651. .platform_data = &asic3_mmc_data,
  652. .pdata_size = sizeof(asic3_mmc_data),
  653. .num_resources = ARRAY_SIZE(asic3_mmc_resources),
  654. .resources = asic3_mmc_resources,
  655. };
  656. static const int clock_ledn[ASIC3_NUM_LEDS] = {
  657. [0] = ASIC3_CLOCK_LED0,
  658. [1] = ASIC3_CLOCK_LED1,
  659. [2] = ASIC3_CLOCK_LED2,
  660. };
  661. static int asic3_leds_enable(struct platform_device *pdev)
  662. {
  663. const struct mfd_cell *cell = mfd_get_cell(pdev);
  664. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  665. asic3_clk_enable(asic, &asic->clocks[clock_ledn[cell->id]]);
  666. return 0;
  667. }
  668. static int asic3_leds_disable(struct platform_device *pdev)
  669. {
  670. const struct mfd_cell *cell = mfd_get_cell(pdev);
  671. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  672. asic3_clk_disable(asic, &asic->clocks[clock_ledn[cell->id]]);
  673. return 0;
  674. }
  675. static int asic3_leds_suspend(struct platform_device *pdev)
  676. {
  677. const struct mfd_cell *cell = mfd_get_cell(pdev);
  678. struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
  679. while (asic3_gpio_get(&asic->gpio, ASIC3_GPIO(C, cell->id)) != 0)
  680. msleep(1);
  681. asic3_clk_disable(asic, &asic->clocks[clock_ledn[cell->id]]);
  682. return 0;
  683. }
  684. static struct mfd_cell asic3_cell_leds[ASIC3_NUM_LEDS] = {
  685. [0] = {
  686. .name = "leds-asic3",
  687. .id = 0,
  688. .enable = asic3_leds_enable,
  689. .disable = asic3_leds_disable,
  690. .suspend = asic3_leds_suspend,
  691. .resume = asic3_leds_enable,
  692. },
  693. [1] = {
  694. .name = "leds-asic3",
  695. .id = 1,
  696. .enable = asic3_leds_enable,
  697. .disable = asic3_leds_disable,
  698. .suspend = asic3_leds_suspend,
  699. .resume = asic3_leds_enable,
  700. },
  701. [2] = {
  702. .name = "leds-asic3",
  703. .id = 2,
  704. .enable = asic3_leds_enable,
  705. .disable = asic3_leds_disable,
  706. .suspend = asic3_leds_suspend,
  707. .resume = asic3_leds_enable,
  708. },
  709. };
  710. static int __init asic3_mfd_probe(struct platform_device *pdev,
  711. struct asic3_platform_data *pdata,
  712. struct resource *mem)
  713. {
  714. struct asic3 *asic = platform_get_drvdata(pdev);
  715. struct resource *mem_sdio;
  716. int irq, ret;
  717. mem_sdio = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  718. if (!mem_sdio)
  719. dev_dbg(asic->dev, "no SDIO MEM resource\n");
  720. irq = platform_get_irq(pdev, 1);
  721. if (irq < 0)
  722. dev_dbg(asic->dev, "no SDIO IRQ resource\n");
  723. /* DS1WM */
  724. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  725. ASIC3_EXTCF_OWM_SMB, 0);
  726. ds1wm_resources[0].start >>= asic->bus_shift;
  727. ds1wm_resources[0].end >>= asic->bus_shift;
  728. /* MMC */
  729. asic->tmio_cnf = ioremap((ASIC3_SD_CONFIG_BASE >> asic->bus_shift) +
  730. mem_sdio->start,
  731. ASIC3_SD_CONFIG_SIZE >> asic->bus_shift);
  732. if (!asic->tmio_cnf) {
  733. ret = -ENOMEM;
  734. dev_dbg(asic->dev, "Couldn't ioremap SD_CONFIG\n");
  735. goto out;
  736. }
  737. asic3_mmc_resources[0].start >>= asic->bus_shift;
  738. asic3_mmc_resources[0].end >>= asic->bus_shift;
  739. ret = mfd_add_devices(&pdev->dev, pdev->id,
  740. &asic3_cell_ds1wm, 1, mem, asic->irq_base);
  741. if (ret < 0)
  742. goto out;
  743. if (mem_sdio && (irq >= 0)) {
  744. ret = mfd_add_devices(&pdev->dev, pdev->id,
  745. &asic3_cell_mmc, 1, mem_sdio, irq);
  746. if (ret < 0)
  747. goto out;
  748. }
  749. if (pdata->leds) {
  750. int i;
  751. for (i = 0; i < ASIC3_NUM_LEDS; ++i) {
  752. asic3_cell_leds[i].platform_data = &pdata->leds[i];
  753. asic3_cell_leds[i].pdata_size = sizeof(pdata->leds[i]);
  754. }
  755. ret = mfd_add_devices(&pdev->dev, 0,
  756. asic3_cell_leds, ASIC3_NUM_LEDS, NULL, 0);
  757. }
  758. out:
  759. return ret;
  760. }
  761. static void asic3_mfd_remove(struct platform_device *pdev)
  762. {
  763. struct asic3 *asic = platform_get_drvdata(pdev);
  764. mfd_remove_devices(&pdev->dev);
  765. iounmap(asic->tmio_cnf);
  766. }
  767. /* Core */
  768. static int __init asic3_probe(struct platform_device *pdev)
  769. {
  770. struct asic3_platform_data *pdata = pdev->dev.platform_data;
  771. struct asic3 *asic;
  772. struct resource *mem;
  773. unsigned long clksel;
  774. int ret = 0;
  775. asic = kzalloc(sizeof(struct asic3), GFP_KERNEL);
  776. if (asic == NULL) {
  777. printk(KERN_ERR "kzalloc failed\n");
  778. return -ENOMEM;
  779. }
  780. spin_lock_init(&asic->lock);
  781. platform_set_drvdata(pdev, asic);
  782. asic->dev = &pdev->dev;
  783. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  784. if (!mem) {
  785. ret = -ENOMEM;
  786. dev_err(asic->dev, "no MEM resource\n");
  787. goto out_free;
  788. }
  789. asic->mapping = ioremap(mem->start, resource_size(mem));
  790. if (!asic->mapping) {
  791. ret = -ENOMEM;
  792. dev_err(asic->dev, "Couldn't ioremap\n");
  793. goto out_free;
  794. }
  795. asic->irq_base = pdata->irq_base;
  796. /* calculate bus shift from mem resource */
  797. asic->bus_shift = 2 - (resource_size(mem) >> 12);
  798. clksel = 0;
  799. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);
  800. ret = asic3_irq_probe(pdev);
  801. if (ret < 0) {
  802. dev_err(asic->dev, "Couldn't probe IRQs\n");
  803. goto out_unmap;
  804. }
  805. asic->gpio.label = "asic3";
  806. asic->gpio.base = pdata->gpio_base;
  807. asic->gpio.ngpio = ASIC3_NUM_GPIOS;
  808. asic->gpio.get = asic3_gpio_get;
  809. asic->gpio.set = asic3_gpio_set;
  810. asic->gpio.direction_input = asic3_gpio_direction_input;
  811. asic->gpio.direction_output = asic3_gpio_direction_output;
  812. asic->gpio.to_irq = asic3_gpio_to_irq;
  813. ret = asic3_gpio_probe(pdev,
  814. pdata->gpio_config,
  815. pdata->gpio_config_num);
  816. if (ret < 0) {
  817. dev_err(asic->dev, "GPIO probe failed\n");
  818. goto out_irq;
  819. }
  820. /* Making a per-device copy is only needed for the
  821. * theoretical case of multiple ASIC3s on one board:
  822. */
  823. memcpy(asic->clocks, asic3_clk_init, sizeof(asic3_clk_init));
  824. asic3_mfd_probe(pdev, pdata, mem);
  825. dev_info(asic->dev, "ASIC3 Core driver\n");
  826. return 0;
  827. out_irq:
  828. asic3_irq_remove(pdev);
  829. out_unmap:
  830. iounmap(asic->mapping);
  831. out_free:
  832. kfree(asic);
  833. return ret;
  834. }
  835. static int __devexit asic3_remove(struct platform_device *pdev)
  836. {
  837. int ret;
  838. struct asic3 *asic = platform_get_drvdata(pdev);
  839. asic3_mfd_remove(pdev);
  840. ret = asic3_gpio_remove(pdev);
  841. if (ret < 0)
  842. return ret;
  843. asic3_irq_remove(pdev);
  844. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);
  845. iounmap(asic->mapping);
  846. kfree(asic);
  847. return 0;
  848. }
  849. static void asic3_shutdown(struct platform_device *pdev)
  850. {
  851. }
  852. static struct platform_driver asic3_device_driver = {
  853. .driver = {
  854. .name = "asic3",
  855. },
  856. .remove = __devexit_p(asic3_remove),
  857. .shutdown = asic3_shutdown,
  858. };
  859. static int __init asic3_init(void)
  860. {
  861. int retval = 0;
  862. retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
  863. return retval;
  864. }
  865. subsys_initcall(asic3_init);