asic3.c 25 KB

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