asic3.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 <[email protected]>,
  15. * Samuel Ortiz <[email protected]>
  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 = gpiochip_get_data(chip);
  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 = gpiochip_get_data(chip);
  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,
  421. gpio_base + ASIC3_GPIO_STATUS) & mask);
  422. }
  423. static void asic3_gpio_set(struct gpio_chip *chip,
  424. unsigned offset, int value)
  425. {
  426. u32 mask, out_reg;
  427. unsigned int gpio_base;
  428. unsigned long flags;
  429. struct asic3 *asic;
  430. asic = gpiochip_get_data(chip);
  431. gpio_base = ASIC3_GPIO_TO_BASE(offset);
  432. if (gpio_base > ASIC3_GPIO_D_BASE) {
  433. dev_err(asic->dev, "Invalid base (0x%x) for gpio %d\n",
  434. gpio_base, offset);
  435. return;
  436. }
  437. mask = ASIC3_GPIO_TO_MASK(offset);
  438. spin_lock_irqsave(&asic->lock, flags);
  439. out_reg = asic3_read_register(asic, gpio_base + ASIC3_GPIO_OUT);
  440. if (value)
  441. out_reg |= mask;
  442. else
  443. out_reg &= ~mask;
  444. asic3_write_register(asic, gpio_base + ASIC3_GPIO_OUT, out_reg);
  445. spin_unlock_irqrestore(&asic->lock, flags);
  446. }
  447. static int asic3_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  448. {
  449. struct asic3 *asic = gpiochip_get_data(chip);
  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_data(&asic->gpio, asic);
  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. usleep_range(1000, 5000);
  552. /* Reset and enable DS1WM */
  553. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  554. ASIC3_EXTCF_OWM_RESET, 1);
  555. usleep_range(1000, 5000);
  556. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, RESET),
  557. ASIC3_EXTCF_OWM_RESET, 0);
  558. usleep_range(1000, 5000);
  559. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  560. ASIC3_EXTCF_OWM_EN, 1);
  561. usleep_range(1000, 5000);
  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. usleep_range(1000, 5000);
  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. usleep_range(1000, 5000);
  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. usleep_range(1000, 5000);
  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 >>
  743. asic->bus_shift) + 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)
  796. return -ENOMEM;
  797. spin_lock_init(&asic->lock);
  798. platform_set_drvdata(pdev, asic);
  799. asic->dev = &pdev->dev;
  800. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  801. if (!mem) {
  802. dev_err(asic->dev, "no MEM resource\n");
  803. return -ENOMEM;
  804. }
  805. asic->mapping = ioremap(mem->start, resource_size(mem));
  806. if (!asic->mapping) {
  807. dev_err(asic->dev, "Couldn't ioremap\n");
  808. return -ENOMEM;
  809. }
  810. asic->irq_base = pdata->irq_base;
  811. /* calculate bus shift from mem resource */
  812. asic->bus_shift = 2 - (resource_size(mem) >> 12);
  813. clksel = 0;
  814. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), clksel);
  815. ret = asic3_irq_probe(pdev);
  816. if (ret < 0) {
  817. dev_err(asic->dev, "Couldn't probe IRQs\n");
  818. goto out_unmap;
  819. }
  820. asic->gpio.label = "asic3";
  821. asic->gpio.base = pdata->gpio_base;
  822. asic->gpio.ngpio = ASIC3_NUM_GPIOS;
  823. asic->gpio.get = asic3_gpio_get;
  824. asic->gpio.set = asic3_gpio_set;
  825. asic->gpio.direction_input = asic3_gpio_direction_input;
  826. asic->gpio.direction_output = asic3_gpio_direction_output;
  827. asic->gpio.to_irq = asic3_gpio_to_irq;
  828. ret = asic3_gpio_probe(pdev,
  829. pdata->gpio_config,
  830. pdata->gpio_config_num);
  831. if (ret < 0) {
  832. dev_err(asic->dev, "GPIO probe failed\n");
  833. goto out_irq;
  834. }
  835. /* Making a per-device copy is only needed for the
  836. * theoretical case of multiple ASIC3s on one board:
  837. */
  838. memcpy(asic->clocks, asic3_clk_init, sizeof(asic3_clk_init));
  839. asic3_mfd_probe(pdev, pdata, mem);
  840. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  841. (ASIC3_EXTCF_CF0_BUF_EN|ASIC3_EXTCF_CF0_PWAIT_EN), 1);
  842. dev_info(asic->dev, "ASIC3 Core driver\n");
  843. return 0;
  844. out_irq:
  845. asic3_irq_remove(pdev);
  846. out_unmap:
  847. iounmap(asic->mapping);
  848. return ret;
  849. }
  850. static int asic3_remove(struct platform_device *pdev)
  851. {
  852. int ret;
  853. struct asic3 *asic = platform_get_drvdata(pdev);
  854. asic3_set_register(asic, ASIC3_OFFSET(EXTCF, SELECT),
  855. (ASIC3_EXTCF_CF0_BUF_EN|ASIC3_EXTCF_CF0_PWAIT_EN), 0);
  856. asic3_mfd_remove(pdev);
  857. ret = asic3_gpio_remove(pdev);
  858. if (ret < 0)
  859. return ret;
  860. asic3_irq_remove(pdev);
  861. asic3_write_register(asic, ASIC3_OFFSET(CLOCK, SEL), 0);
  862. iounmap(asic->mapping);
  863. return 0;
  864. }
  865. static void asic3_shutdown(struct platform_device *pdev)
  866. {
  867. }
  868. static struct platform_driver asic3_device_driver = {
  869. .driver = {
  870. .name = "asic3",
  871. },
  872. .remove = asic3_remove,
  873. .shutdown = asic3_shutdown,
  874. };
  875. static int __init asic3_init(void)
  876. {
  877. int retval = 0;
  878. retval = platform_driver_probe(&asic3_device_driver, asic3_probe);
  879. return retval;
  880. }
  881. subsys_initcall(asic3_init);