locomo.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /*
  2. * linux/arch/arm/common/locomo.c
  3. *
  4. * Sharp LoCoMo 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. * This file contains all generic LoCoMo support.
  11. *
  12. * All initialization functions provided here are intended to be called
  13. * from machine specific code with proper arguments when required.
  14. *
  15. * Based on sa1111.c
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/device.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/hardware.h>
  28. #include <asm/mach-types.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/mach/irq.h>
  32. #include <asm/hardware/locomo.h>
  33. /* M62332 output channel selection */
  34. #define M62332_EVR_CH 1 /* M62332 volume channel number */
  35. /* 0 : CH.1 , 1 : CH. 2 */
  36. /* DAC send data */
  37. #define M62332_SLAVE_ADDR 0x4e /* Slave address */
  38. #define M62332_W_BIT 0x00 /* W bit (0 only) */
  39. #define M62332_SUB_ADDR 0x00 /* Sub address */
  40. #define M62332_A_BIT 0x00 /* A bit (0 only) */
  41. /* DAC setup and hold times (expressed in us) */
  42. #define DAC_BUS_FREE_TIME 5 /* 4.7 us */
  43. #define DAC_START_SETUP_TIME 5 /* 4.7 us */
  44. #define DAC_STOP_SETUP_TIME 4 /* 4.0 us */
  45. #define DAC_START_HOLD_TIME 5 /* 4.7 us */
  46. #define DAC_SCL_LOW_HOLD_TIME 5 /* 4.7 us */
  47. #define DAC_SCL_HIGH_HOLD_TIME 4 /* 4.0 us */
  48. #define DAC_DATA_SETUP_TIME 1 /* 250 ns */
  49. #define DAC_DATA_HOLD_TIME 1 /* 300 ns */
  50. #define DAC_LOW_SETUP_TIME 1 /* 300 ns */
  51. #define DAC_HIGH_SETUP_TIME 1 /* 1000 ns */
  52. /* the following is the overall data for the locomo chip */
  53. struct locomo {
  54. struct device *dev;
  55. unsigned long phys;
  56. unsigned int irq;
  57. spinlock_t lock;
  58. void *base;
  59. };
  60. struct locomo_dev_info {
  61. unsigned long offset;
  62. unsigned long length;
  63. unsigned int devid;
  64. unsigned int irq[1];
  65. const char * name;
  66. };
  67. /* All the locomo devices. If offset is non-zero, the mapbase for the
  68. * locomo_dev will be set to the chip base plus offset. If offset is
  69. * zero, then the mapbase for the locomo_dev will be set to zero. An
  70. * offset of zero means the device only uses GPIOs or other helper
  71. * functions inside this file */
  72. static struct locomo_dev_info locomo_devices[] = {
  73. {
  74. .devid = LOCOMO_DEVID_KEYBOARD,
  75. .irq = {
  76. IRQ_LOCOMO_KEY,
  77. },
  78. .name = "locomo-keyboard",
  79. .offset = LOCOMO_KEYBOARD,
  80. .length = 16,
  81. },
  82. {
  83. .devid = LOCOMO_DEVID_FRONTLIGHT,
  84. .irq = {},
  85. .name = "locomo-frontlight",
  86. .offset = LOCOMO_FRONTLIGHT,
  87. .length = 8,
  88. },
  89. {
  90. .devid = LOCOMO_DEVID_BACKLIGHT,
  91. .irq = {},
  92. .name = "locomo-backlight",
  93. .offset = LOCOMO_BACKLIGHT,
  94. .length = 8,
  95. },
  96. {
  97. .devid = LOCOMO_DEVID_AUDIO,
  98. .irq = {},
  99. .name = "locomo-audio",
  100. .offset = LOCOMO_AUDIO,
  101. .length = 4,
  102. },
  103. {
  104. .devid = LOCOMO_DEVID_LED,
  105. .irq = {},
  106. .name = "locomo-led",
  107. .offset = LOCOMO_LED,
  108. .length = 8,
  109. },
  110. {
  111. .devid = LOCOMO_DEVID_UART,
  112. .irq = {},
  113. .name = "locomo-uart",
  114. .offset = 0,
  115. .length = 0,
  116. },
  117. };
  118. /** LoCoMo interrupt handling stuff.
  119. * NOTE: LoCoMo has a 1 to many mapping on all of its IRQs.
  120. * that is, there is only one real hardware interrupt
  121. * we determine which interrupt it is by reading some IO memory.
  122. * We have two levels of expansion, first in the handler for the
  123. * hardware interrupt we generate an interrupt
  124. * IRQ_LOCOMO_*_BASE and those handlers generate more interrupts
  125. *
  126. * hardware irq reads LOCOMO_ICR & 0x0f00
  127. * IRQ_LOCOMO_KEY_BASE
  128. * IRQ_LOCOMO_GPIO_BASE
  129. * IRQ_LOCOMO_LT_BASE
  130. * IRQ_LOCOMO_SPI_BASE
  131. * IRQ_LOCOMO_KEY_BASE reads LOCOMO_KIC & 0x0001
  132. * IRQ_LOCOMO_KEY
  133. * IRQ_LOCOMO_GPIO_BASE reads LOCOMO_GIR & LOCOMO_GPD & 0xffff
  134. * IRQ_LOCOMO_GPIO[0-15]
  135. * IRQ_LOCOMO_LT_BASE reads LOCOMO_LTINT & 0x0001
  136. * IRQ_LOCOMO_LT
  137. * IRQ_LOCOMO_SPI_BASE reads LOCOMO_SPIIR & 0x000F
  138. * IRQ_LOCOMO_SPI_RFR
  139. * IRQ_LOCOMO_SPI_RFW
  140. * IRQ_LOCOMO_SPI_OVRN
  141. * IRQ_LOCOMO_SPI_TEND
  142. */
  143. #define LOCOMO_IRQ_START (IRQ_LOCOMO_KEY_BASE)
  144. #define LOCOMO_IRQ_KEY_START (IRQ_LOCOMO_KEY)
  145. #define LOCOMO_IRQ_GPIO_START (IRQ_LOCOMO_GPIO0)
  146. #define LOCOMO_IRQ_LT_START (IRQ_LOCOMO_LT)
  147. #define LOCOMO_IRQ_SPI_START (IRQ_LOCOMO_SPI_RFR)
  148. static void locomo_handler(unsigned int irq, struct irqdesc *desc,
  149. struct pt_regs *regs)
  150. {
  151. int req, i;
  152. struct irqdesc *d;
  153. void *mapbase = get_irq_chipdata(irq);
  154. /* Acknowledge the parent IRQ */
  155. desc->chip->ack(irq);
  156. /* check why this interrupt was generated */
  157. req = locomo_readl(mapbase + LOCOMO_ICR) & 0x0f00;
  158. if (req) {
  159. /* generate the next interrupt(s) */
  160. irq = LOCOMO_IRQ_START;
  161. d = irq_desc + irq;
  162. for (i = 0; i <= 3; i++, d++, irq++) {
  163. if (req & (0x0100 << i)) {
  164. d->handle(irq, d, regs);
  165. }
  166. }
  167. }
  168. }
  169. static void locomo_ack_irq(unsigned int irq)
  170. {
  171. }
  172. static void locomo_mask_irq(unsigned int irq)
  173. {
  174. void *mapbase = get_irq_chipdata(irq);
  175. unsigned int r;
  176. r = locomo_readl(mapbase + LOCOMO_ICR);
  177. r &= ~(0x0010 << (irq - LOCOMO_IRQ_START));
  178. locomo_writel(r, mapbase + LOCOMO_ICR);
  179. }
  180. static void locomo_unmask_irq(unsigned int irq)
  181. {
  182. void *mapbase = get_irq_chipdata(irq);
  183. unsigned int r;
  184. r = locomo_readl(mapbase + LOCOMO_ICR);
  185. r |= (0x0010 << (irq - LOCOMO_IRQ_START));
  186. locomo_writel(r, mapbase + LOCOMO_ICR);
  187. }
  188. static struct irqchip locomo_chip = {
  189. .ack = locomo_ack_irq,
  190. .mask = locomo_mask_irq,
  191. .unmask = locomo_unmask_irq,
  192. };
  193. static void locomo_key_handler(unsigned int irq, struct irqdesc *desc,
  194. struct pt_regs *regs)
  195. {
  196. struct irqdesc *d;
  197. void *mapbase = get_irq_chipdata(irq);
  198. if (locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC) & 0x0001) {
  199. d = irq_desc + LOCOMO_IRQ_KEY_START;
  200. d->handle(LOCOMO_IRQ_KEY_START, d, regs);
  201. }
  202. }
  203. static void locomo_key_ack_irq(unsigned int irq)
  204. {
  205. void *mapbase = get_irq_chipdata(irq);
  206. unsigned int r;
  207. r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  208. r &= ~(0x0100 << (irq - LOCOMO_IRQ_KEY_START));
  209. locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  210. }
  211. static void locomo_key_mask_irq(unsigned int irq)
  212. {
  213. void *mapbase = get_irq_chipdata(irq);
  214. unsigned int r;
  215. r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  216. r &= ~(0x0010 << (irq - LOCOMO_IRQ_KEY_START));
  217. locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  218. }
  219. static void locomo_key_unmask_irq(unsigned int irq)
  220. {
  221. void *mapbase = get_irq_chipdata(irq);
  222. unsigned int r;
  223. r = locomo_readl(mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  224. r |= (0x0010 << (irq - LOCOMO_IRQ_KEY_START));
  225. locomo_writel(r, mapbase + LOCOMO_KEYBOARD + LOCOMO_KIC);
  226. }
  227. static struct irqchip locomo_key_chip = {
  228. .ack = locomo_key_ack_irq,
  229. .mask = locomo_key_mask_irq,
  230. .unmask = locomo_key_unmask_irq,
  231. };
  232. static void locomo_gpio_handler(unsigned int irq, struct irqdesc *desc,
  233. struct pt_regs *regs)
  234. {
  235. int req, i;
  236. struct irqdesc *d;
  237. void *mapbase = get_irq_chipdata(irq);
  238. req = locomo_readl(mapbase + LOCOMO_GIR) &
  239. locomo_readl(mapbase + LOCOMO_GPD) &
  240. 0xffff;
  241. if (req) {
  242. irq = LOCOMO_IRQ_GPIO_START;
  243. d = irq_desc + LOCOMO_IRQ_GPIO_START;
  244. for (i = 0; i <= 15; i++, irq++, d++) {
  245. if (req & (0x0001 << i)) {
  246. d->handle(irq, d, regs);
  247. }
  248. }
  249. }
  250. }
  251. static void locomo_gpio_ack_irq(unsigned int irq)
  252. {
  253. void *mapbase = get_irq_chipdata(irq);
  254. unsigned int r;
  255. r = locomo_readl(mapbase + LOCOMO_GWE);
  256. r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
  257. locomo_writel(r, mapbase + LOCOMO_GWE);
  258. r = locomo_readl(mapbase + LOCOMO_GIS);
  259. r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
  260. locomo_writel(r, mapbase + LOCOMO_GIS);
  261. r = locomo_readl(mapbase + LOCOMO_GWE);
  262. r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
  263. locomo_writel(r, mapbase + LOCOMO_GWE);
  264. }
  265. static void locomo_gpio_mask_irq(unsigned int irq)
  266. {
  267. void *mapbase = get_irq_chipdata(irq);
  268. unsigned int r;
  269. r = locomo_readl(mapbase + LOCOMO_GIE);
  270. r &= ~(0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
  271. locomo_writel(r, mapbase + LOCOMO_GIE);
  272. }
  273. static void locomo_gpio_unmask_irq(unsigned int irq)
  274. {
  275. void *mapbase = get_irq_chipdata(irq);
  276. unsigned int r;
  277. r = locomo_readl(mapbase + LOCOMO_GIE);
  278. r |= (0x0001 << (irq - LOCOMO_IRQ_GPIO_START));
  279. locomo_writel(r, mapbase + LOCOMO_GIE);
  280. }
  281. static struct irqchip locomo_gpio_chip = {
  282. .ack = locomo_gpio_ack_irq,
  283. .mask = locomo_gpio_mask_irq,
  284. .unmask = locomo_gpio_unmask_irq,
  285. };
  286. static void locomo_lt_handler(unsigned int irq, struct irqdesc *desc,
  287. struct pt_regs *regs)
  288. {
  289. struct irqdesc *d;
  290. void *mapbase = get_irq_chipdata(irq);
  291. if (locomo_readl(mapbase + LOCOMO_LTINT) & 0x0001) {
  292. d = irq_desc + LOCOMO_IRQ_LT_START;
  293. d->handle(LOCOMO_IRQ_LT_START, d, regs);
  294. }
  295. }
  296. static void locomo_lt_ack_irq(unsigned int irq)
  297. {
  298. void *mapbase = get_irq_chipdata(irq);
  299. unsigned int r;
  300. r = locomo_readl(mapbase + LOCOMO_LTINT);
  301. r &= ~(0x0100 << (irq - LOCOMO_IRQ_LT_START));
  302. locomo_writel(r, mapbase + LOCOMO_LTINT);
  303. }
  304. static void locomo_lt_mask_irq(unsigned int irq)
  305. {
  306. void *mapbase = get_irq_chipdata(irq);
  307. unsigned int r;
  308. r = locomo_readl(mapbase + LOCOMO_LTINT);
  309. r &= ~(0x0010 << (irq - LOCOMO_IRQ_LT_START));
  310. locomo_writel(r, mapbase + LOCOMO_LTINT);
  311. }
  312. static void locomo_lt_unmask_irq(unsigned int irq)
  313. {
  314. void *mapbase = get_irq_chipdata(irq);
  315. unsigned int r;
  316. r = locomo_readl(mapbase + LOCOMO_LTINT);
  317. r |= (0x0010 << (irq - LOCOMO_IRQ_LT_START));
  318. locomo_writel(r, mapbase + LOCOMO_LTINT);
  319. }
  320. static struct irqchip locomo_lt_chip = {
  321. .ack = locomo_lt_ack_irq,
  322. .mask = locomo_lt_mask_irq,
  323. .unmask = locomo_lt_unmask_irq,
  324. };
  325. static void locomo_spi_handler(unsigned int irq, struct irqdesc *desc,
  326. struct pt_regs *regs)
  327. {
  328. int req, i;
  329. struct irqdesc *d;
  330. void *mapbase = get_irq_chipdata(irq);
  331. req = locomo_readl(mapbase + LOCOMO_SPIIR) & 0x000F;
  332. if (req) {
  333. irq = LOCOMO_IRQ_SPI_START;
  334. d = irq_desc + irq;
  335. for (i = 0; i <= 3; i++, irq++, d++) {
  336. if (req & (0x0001 << i)) {
  337. d->handle(irq, d, regs);
  338. }
  339. }
  340. }
  341. }
  342. static void locomo_spi_ack_irq(unsigned int irq)
  343. {
  344. void *mapbase = get_irq_chipdata(irq);
  345. unsigned int r;
  346. r = locomo_readl(mapbase + LOCOMO_SPIWE);
  347. r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
  348. locomo_writel(r, mapbase + LOCOMO_SPIWE);
  349. r = locomo_readl(mapbase + LOCOMO_SPIIS);
  350. r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
  351. locomo_writel(r, mapbase + LOCOMO_SPIIS);
  352. r = locomo_readl(mapbase + LOCOMO_SPIWE);
  353. r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
  354. locomo_writel(r, mapbase + LOCOMO_SPIWE);
  355. }
  356. static void locomo_spi_mask_irq(unsigned int irq)
  357. {
  358. void *mapbase = get_irq_chipdata(irq);
  359. unsigned int r;
  360. r = locomo_readl(mapbase + LOCOMO_SPIIE);
  361. r &= ~(0x0001 << (irq - LOCOMO_IRQ_SPI_START));
  362. locomo_writel(r, mapbase + LOCOMO_SPIIE);
  363. }
  364. static void locomo_spi_unmask_irq(unsigned int irq)
  365. {
  366. void *mapbase = get_irq_chipdata(irq);
  367. unsigned int r;
  368. r = locomo_readl(mapbase + LOCOMO_SPIIE);
  369. r |= (0x0001 << (irq - LOCOMO_IRQ_SPI_START));
  370. locomo_writel(r, mapbase + LOCOMO_SPIIE);
  371. }
  372. static struct irqchip locomo_spi_chip = {
  373. .ack = locomo_spi_ack_irq,
  374. .mask = locomo_spi_mask_irq,
  375. .unmask = locomo_spi_unmask_irq,
  376. };
  377. static void locomo_setup_irq(struct locomo *lchip)
  378. {
  379. int irq;
  380. void *irqbase = lchip->base;
  381. /*
  382. * Install handler for IRQ_LOCOMO_HW.
  383. */
  384. set_irq_type(lchip->irq, IRQT_FALLING);
  385. set_irq_chipdata(lchip->irq, irqbase);
  386. set_irq_chained_handler(lchip->irq, locomo_handler);
  387. /* Install handlers for IRQ_LOCOMO_*_BASE */
  388. set_irq_chip(IRQ_LOCOMO_KEY_BASE, &locomo_chip);
  389. set_irq_chipdata(IRQ_LOCOMO_KEY_BASE, irqbase);
  390. set_irq_chained_handler(IRQ_LOCOMO_KEY_BASE, locomo_key_handler);
  391. set_irq_flags(IRQ_LOCOMO_KEY_BASE, IRQF_VALID | IRQF_PROBE);
  392. set_irq_chip(IRQ_LOCOMO_GPIO_BASE, &locomo_chip);
  393. set_irq_chipdata(IRQ_LOCOMO_GPIO_BASE, irqbase);
  394. set_irq_chained_handler(IRQ_LOCOMO_GPIO_BASE, locomo_gpio_handler);
  395. set_irq_flags(IRQ_LOCOMO_GPIO_BASE, IRQF_VALID | IRQF_PROBE);
  396. set_irq_chip(IRQ_LOCOMO_LT_BASE, &locomo_chip);
  397. set_irq_chipdata(IRQ_LOCOMO_LT_BASE, irqbase);
  398. set_irq_chained_handler(IRQ_LOCOMO_LT_BASE, locomo_lt_handler);
  399. set_irq_flags(IRQ_LOCOMO_LT_BASE, IRQF_VALID | IRQF_PROBE);
  400. set_irq_chip(IRQ_LOCOMO_SPI_BASE, &locomo_chip);
  401. set_irq_chipdata(IRQ_LOCOMO_SPI_BASE, irqbase);
  402. set_irq_chained_handler(IRQ_LOCOMO_SPI_BASE, locomo_spi_handler);
  403. set_irq_flags(IRQ_LOCOMO_SPI_BASE, IRQF_VALID | IRQF_PROBE);
  404. /* install handlers for IRQ_LOCOMO_KEY_BASE generated interrupts */
  405. set_irq_chip(LOCOMO_IRQ_KEY_START, &locomo_key_chip);
  406. set_irq_chipdata(LOCOMO_IRQ_KEY_START, irqbase);
  407. set_irq_handler(LOCOMO_IRQ_KEY_START, do_edge_IRQ);
  408. set_irq_flags(LOCOMO_IRQ_KEY_START, IRQF_VALID | IRQF_PROBE);
  409. /* install handlers for IRQ_LOCOMO_GPIO_BASE generated interrupts */
  410. for (irq = LOCOMO_IRQ_GPIO_START; irq < LOCOMO_IRQ_GPIO_START + 16; irq++) {
  411. set_irq_chip(irq, &locomo_gpio_chip);
  412. set_irq_chipdata(irq, irqbase);
  413. set_irq_handler(irq, do_edge_IRQ);
  414. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  415. }
  416. /* install handlers for IRQ_LOCOMO_LT_BASE generated interrupts */
  417. set_irq_chip(LOCOMO_IRQ_LT_START, &locomo_lt_chip);
  418. set_irq_chipdata(LOCOMO_IRQ_LT_START, irqbase);
  419. set_irq_handler(LOCOMO_IRQ_LT_START, do_edge_IRQ);
  420. set_irq_flags(LOCOMO_IRQ_LT_START, IRQF_VALID | IRQF_PROBE);
  421. /* install handlers for IRQ_LOCOMO_SPI_BASE generated interrupts */
  422. for (irq = LOCOMO_IRQ_SPI_START; irq < LOCOMO_IRQ_SPI_START + 3; irq++) {
  423. set_irq_chip(irq, &locomo_spi_chip);
  424. set_irq_chipdata(irq, irqbase);
  425. set_irq_handler(irq, do_edge_IRQ);
  426. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  427. }
  428. }
  429. static void locomo_dev_release(struct device *_dev)
  430. {
  431. struct locomo_dev *dev = LOCOMO_DEV(_dev);
  432. kfree(dev);
  433. }
  434. static int
  435. locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
  436. {
  437. struct locomo_dev *dev;
  438. int ret;
  439. dev = kmalloc(sizeof(struct locomo_dev), GFP_KERNEL);
  440. if (!dev) {
  441. ret = -ENOMEM;
  442. goto out;
  443. }
  444. memset(dev, 0, sizeof(struct locomo_dev));
  445. strncpy(dev->dev.bus_id,info->name,sizeof(dev->dev.bus_id));
  446. /*
  447. * If the parent device has a DMA mask associated with it,
  448. * propagate it down to the children.
  449. */
  450. if (lchip->dev->dma_mask) {
  451. dev->dma_mask = *lchip->dev->dma_mask;
  452. dev->dev.dma_mask = &dev->dma_mask;
  453. }
  454. dev->devid = info->devid;
  455. dev->dev.parent = lchip->dev;
  456. dev->dev.bus = &locomo_bus_type;
  457. dev->dev.release = locomo_dev_release;
  458. dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
  459. if (info->offset)
  460. dev->mapbase = lchip->base + info->offset;
  461. else
  462. dev->mapbase = 0;
  463. dev->length = info->length;
  464. memmove(dev->irq, info->irq, sizeof(dev->irq));
  465. ret = device_register(&dev->dev);
  466. if (ret) {
  467. out:
  468. kfree(dev);
  469. }
  470. return ret;
  471. }
  472. /**
  473. * locomo_probe - probe for a single LoCoMo chip.
  474. * @phys_addr: physical address of device.
  475. *
  476. * Probe for a LoCoMo chip. This must be called
  477. * before any other locomo-specific code.
  478. *
  479. * Returns:
  480. * %-ENODEV device not found.
  481. * %-EBUSY physical address already marked in-use.
  482. * %0 successful.
  483. */
  484. static int
  485. __locomo_probe(struct device *me, struct resource *mem, int irq)
  486. {
  487. struct locomo *lchip;
  488. unsigned long r;
  489. int i, ret = -ENODEV;
  490. lchip = kmalloc(sizeof(struct locomo), GFP_KERNEL);
  491. if (!lchip)
  492. return -ENOMEM;
  493. memset(lchip, 0, sizeof(struct locomo));
  494. spin_lock_init(&lchip->lock);
  495. lchip->dev = me;
  496. dev_set_drvdata(lchip->dev, lchip);
  497. lchip->phys = mem->start;
  498. lchip->irq = irq;
  499. /*
  500. * Map the whole region. This also maps the
  501. * registers for our children.
  502. */
  503. lchip->base = ioremap(mem->start, PAGE_SIZE);
  504. if (!lchip->base) {
  505. ret = -ENOMEM;
  506. goto out;
  507. }
  508. /* locomo initialize */
  509. locomo_writel(0, lchip->base + LOCOMO_ICR);
  510. /* KEYBOARD */
  511. locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
  512. /* GPIO */
  513. locomo_writel(0, lchip->base + LOCOMO_GPO);
  514. locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
  515. , lchip->base + LOCOMO_GPE);
  516. locomo_writel( (LOCOMO_GPIO(2) | LOCOMO_GPIO(3) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
  517. , lchip->base + LOCOMO_GPD);
  518. locomo_writel(0, lchip->base + LOCOMO_GIE);
  519. /* FrontLight */
  520. locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
  521. locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
  522. /* Longtime timer */
  523. locomo_writel(0, lchip->base + LOCOMO_LTINT);
  524. /* SPI */
  525. locomo_writel(0, lchip->base + LOCOMO_SPIIE);
  526. locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
  527. r = locomo_readl(lchip->base + LOCOMO_ASD);
  528. r |= 0x8000;
  529. locomo_writel(r, lchip->base + LOCOMO_ASD);
  530. locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
  531. r = locomo_readl(lchip->base + LOCOMO_HSD);
  532. r |= 0x8000;
  533. locomo_writel(r, lchip->base + LOCOMO_HSD);
  534. locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
  535. /* XON */
  536. locomo_writel(0x80, lchip->base + LOCOMO_TADC);
  537. udelay(1000);
  538. /* CLK9MEN */
  539. r = locomo_readl(lchip->base + LOCOMO_TADC);
  540. r |= 0x10;
  541. locomo_writel(r, lchip->base + LOCOMO_TADC);
  542. udelay(100);
  543. /* init DAC */
  544. r = locomo_readl(lchip->base + LOCOMO_DAC);
  545. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  546. locomo_writel(r, lchip->base + LOCOMO_DAC);
  547. r = locomo_readl(lchip->base + LOCOMO_VER);
  548. printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
  549. /*
  550. * The interrupt controller must be initialised before any
  551. * other device to ensure that the interrupts are available.
  552. */
  553. if (lchip->irq != NO_IRQ)
  554. locomo_setup_irq(lchip);
  555. for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
  556. locomo_init_one_child(lchip, &locomo_devices[i]);
  557. return 0;
  558. out:
  559. kfree(lchip);
  560. return ret;
  561. }
  562. static void __locomo_remove(struct locomo *lchip)
  563. {
  564. struct list_head *l, *n;
  565. list_for_each_safe(l, n, &lchip->dev->children) {
  566. struct device *d = list_to_dev(l);
  567. device_unregister(d);
  568. }
  569. if (lchip->irq != NO_IRQ) {
  570. set_irq_chained_handler(lchip->irq, NULL);
  571. set_irq_data(lchip->irq, NULL);
  572. }
  573. iounmap(lchip->base);
  574. kfree(lchip);
  575. }
  576. static int locomo_probe(struct device *dev)
  577. {
  578. struct platform_device *pdev = to_platform_device(dev);
  579. struct resource *mem;
  580. int irq;
  581. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  582. if (!mem)
  583. return -EINVAL;
  584. irq = platform_get_irq(pdev, 0);
  585. return __locomo_probe(dev, mem, irq);
  586. }
  587. static int locomo_remove(struct device *dev)
  588. {
  589. struct locomo *lchip = dev_get_drvdata(dev);
  590. if (lchip) {
  591. __locomo_remove(lchip);
  592. dev_set_drvdata(dev, NULL);
  593. }
  594. return 0;
  595. }
  596. /*
  597. * Not sure if this should be on the system bus or not yet.
  598. * We really want some way to register a system device at
  599. * the per-machine level, and then have this driver pick
  600. * up the registered devices.
  601. */
  602. static struct device_driver locomo_device_driver = {
  603. .name = "locomo",
  604. .bus = &platform_bus_type,
  605. .probe = locomo_probe,
  606. .remove = locomo_remove,
  607. };
  608. /*
  609. * Get the parent device driver (us) structure
  610. * from a child function device
  611. */
  612. static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
  613. {
  614. return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
  615. }
  616. void locomo_gpio_set_dir(struct locomo_dev *ldev, unsigned int bits, unsigned int dir)
  617. {
  618. struct locomo *lchip = locomo_chip_driver(ldev);
  619. unsigned long flags;
  620. unsigned int r;
  621. spin_lock_irqsave(&lchip->lock, flags);
  622. r = locomo_readl(lchip->base + LOCOMO_GPD);
  623. r &= ~bits;
  624. locomo_writel(r, lchip->base + LOCOMO_GPD);
  625. r = locomo_readl(lchip->base + LOCOMO_GPE);
  626. if (dir)
  627. r |= bits;
  628. else
  629. r &= ~bits;
  630. locomo_writel(r, lchip->base + LOCOMO_GPE);
  631. spin_unlock_irqrestore(&lchip->lock, flags);
  632. }
  633. unsigned int locomo_gpio_read_level(struct locomo_dev *ldev, unsigned int bits)
  634. {
  635. struct locomo *lchip = locomo_chip_driver(ldev);
  636. unsigned long flags;
  637. unsigned int ret;
  638. spin_lock_irqsave(&lchip->lock, flags);
  639. ret = locomo_readl(lchip->base + LOCOMO_GPL);
  640. spin_unlock_irqrestore(&lchip->lock, flags);
  641. ret &= bits;
  642. return ret;
  643. }
  644. unsigned int locomo_gpio_read_output(struct locomo_dev *ldev, unsigned int bits)
  645. {
  646. struct locomo *lchip = locomo_chip_driver(ldev);
  647. unsigned long flags;
  648. unsigned int ret;
  649. spin_lock_irqsave(&lchip->lock, flags);
  650. ret = locomo_readl(lchip->base + LOCOMO_GPO);
  651. spin_unlock_irqrestore(&lchip->lock, flags);
  652. ret &= bits;
  653. return ret;
  654. }
  655. void locomo_gpio_write(struct locomo_dev *ldev, unsigned int bits, unsigned int set)
  656. {
  657. struct locomo *lchip = locomo_chip_driver(ldev);
  658. unsigned long flags;
  659. unsigned int r;
  660. spin_lock_irqsave(&lchip->lock, flags);
  661. r = locomo_readl(lchip->base + LOCOMO_GPO);
  662. if (set)
  663. r |= bits;
  664. else
  665. r &= ~bits;
  666. locomo_writel(r, lchip->base + LOCOMO_GPO);
  667. spin_unlock_irqrestore(&lchip->lock, flags);
  668. }
  669. static void locomo_m62332_sendbit(void *mapbase, int bit)
  670. {
  671. unsigned int r;
  672. r = locomo_readl(mapbase + LOCOMO_DAC);
  673. r &= ~(LOCOMO_DAC_SCLOEB);
  674. locomo_writel(r, mapbase + LOCOMO_DAC);
  675. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  676. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  677. r = locomo_readl(mapbase + LOCOMO_DAC);
  678. r &= ~(LOCOMO_DAC_SCLOEB);
  679. locomo_writel(r, mapbase + LOCOMO_DAC);
  680. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  681. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  682. if (bit & 1) {
  683. r = locomo_readl(mapbase + LOCOMO_DAC);
  684. r |= LOCOMO_DAC_SDAOEB;
  685. locomo_writel(r, mapbase + LOCOMO_DAC);
  686. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  687. } else {
  688. r = locomo_readl(mapbase + LOCOMO_DAC);
  689. r &= ~(LOCOMO_DAC_SDAOEB);
  690. locomo_writel(r, mapbase + LOCOMO_DAC);
  691. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  692. }
  693. udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
  694. r = locomo_readl(mapbase + LOCOMO_DAC);
  695. r |= LOCOMO_DAC_SCLOEB;
  696. locomo_writel(r, mapbase + LOCOMO_DAC);
  697. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  698. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  699. }
  700. void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
  701. {
  702. struct locomo *lchip = locomo_chip_driver(ldev);
  703. int i;
  704. unsigned char data;
  705. unsigned int r;
  706. void *mapbase = lchip->base;
  707. unsigned long flags;
  708. spin_lock_irqsave(&lchip->lock, flags);
  709. /* Start */
  710. udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
  711. r = locomo_readl(mapbase + LOCOMO_DAC);
  712. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  713. locomo_writel(r, mapbase + LOCOMO_DAC);
  714. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  715. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  716. r = locomo_readl(mapbase + LOCOMO_DAC);
  717. r &= ~(LOCOMO_DAC_SDAOEB);
  718. locomo_writel(r, mapbase + LOCOMO_DAC);
  719. udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
  720. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  721. /* Send slave address and W bit (LSB is W bit) */
  722. data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
  723. for (i = 1; i <= 8; i++) {
  724. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  725. }
  726. /* Check A bit */
  727. r = locomo_readl(mapbase + LOCOMO_DAC);
  728. r &= ~(LOCOMO_DAC_SCLOEB);
  729. locomo_writel(r, mapbase + LOCOMO_DAC);
  730. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  731. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  732. r = locomo_readl(mapbase + LOCOMO_DAC);
  733. r &= ~(LOCOMO_DAC_SDAOEB);
  734. locomo_writel(r, mapbase + LOCOMO_DAC);
  735. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  736. r = locomo_readl(mapbase + LOCOMO_DAC);
  737. r |= LOCOMO_DAC_SCLOEB;
  738. locomo_writel(r, mapbase + LOCOMO_DAC);
  739. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  740. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  741. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  742. printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
  743. return;
  744. }
  745. /* Send Sub address (LSB is channel select) */
  746. /* channel = 0 : ch1 select */
  747. /* = 1 : ch2 select */
  748. data = M62332_SUB_ADDR + channel;
  749. for (i = 1; i <= 8; i++) {
  750. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  751. }
  752. /* Check A bit */
  753. r = locomo_readl(mapbase + LOCOMO_DAC);
  754. r &= ~(LOCOMO_DAC_SCLOEB);
  755. locomo_writel(r, mapbase + LOCOMO_DAC);
  756. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  757. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  758. r = locomo_readl(mapbase + LOCOMO_DAC);
  759. r &= ~(LOCOMO_DAC_SDAOEB);
  760. locomo_writel(r, mapbase + LOCOMO_DAC);
  761. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  762. r = locomo_readl(mapbase + LOCOMO_DAC);
  763. r |= LOCOMO_DAC_SCLOEB;
  764. locomo_writel(r, mapbase + LOCOMO_DAC);
  765. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  766. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  767. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  768. printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
  769. return;
  770. }
  771. /* Send DAC data */
  772. for (i = 1; i <= 8; i++) {
  773. locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
  774. }
  775. /* Check A bit */
  776. r = locomo_readl(mapbase + LOCOMO_DAC);
  777. r &= ~(LOCOMO_DAC_SCLOEB);
  778. locomo_writel(r, mapbase + LOCOMO_DAC);
  779. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  780. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  781. r = locomo_readl(mapbase + LOCOMO_DAC);
  782. r &= ~(LOCOMO_DAC_SDAOEB);
  783. locomo_writel(r, mapbase + LOCOMO_DAC);
  784. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  785. r = locomo_readl(mapbase + LOCOMO_DAC);
  786. r |= LOCOMO_DAC_SCLOEB;
  787. locomo_writel(r, mapbase + LOCOMO_DAC);
  788. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  789. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  790. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  791. printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
  792. return;
  793. }
  794. /* stop */
  795. r = locomo_readl(mapbase + LOCOMO_DAC);
  796. r &= ~(LOCOMO_DAC_SCLOEB);
  797. locomo_writel(r, mapbase + LOCOMO_DAC);
  798. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  799. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  800. r = locomo_readl(mapbase + LOCOMO_DAC);
  801. r |= LOCOMO_DAC_SCLOEB;
  802. locomo_writel(r, mapbase + LOCOMO_DAC);
  803. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  804. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  805. r = locomo_readl(mapbase + LOCOMO_DAC);
  806. r |= LOCOMO_DAC_SDAOEB;
  807. locomo_writel(r, mapbase + LOCOMO_DAC);
  808. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  809. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  810. r = locomo_readl(mapbase + LOCOMO_DAC);
  811. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  812. locomo_writel(r, mapbase + LOCOMO_DAC);
  813. udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
  814. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  815. spin_unlock_irqrestore(&lchip->lock, flags);
  816. }
  817. /*
  818. * LoCoMo "Register Access Bus."
  819. *
  820. * We model this as a regular bus type, and hang devices directly
  821. * off this.
  822. */
  823. static int locomo_match(struct device *_dev, struct device_driver *_drv)
  824. {
  825. struct locomo_dev *dev = LOCOMO_DEV(_dev);
  826. struct locomo_driver *drv = LOCOMO_DRV(_drv);
  827. return dev->devid == drv->devid;
  828. }
  829. static int locomo_bus_suspend(struct device *dev, pm_message_t state)
  830. {
  831. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  832. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  833. int ret = 0;
  834. if (drv && drv->suspend)
  835. ret = drv->suspend(ldev, state);
  836. return ret;
  837. }
  838. static int locomo_bus_resume(struct device *dev)
  839. {
  840. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  841. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  842. int ret = 0;
  843. if (drv && drv->resume)
  844. ret = drv->resume(ldev);
  845. return ret;
  846. }
  847. static int locomo_bus_probe(struct device *dev)
  848. {
  849. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  850. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  851. int ret = -ENODEV;
  852. if (drv->probe)
  853. ret = drv->probe(ldev);
  854. return ret;
  855. }
  856. static int locomo_bus_remove(struct device *dev)
  857. {
  858. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  859. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  860. int ret = 0;
  861. if (drv->remove)
  862. ret = drv->remove(ldev);
  863. return ret;
  864. }
  865. struct bus_type locomo_bus_type = {
  866. .name = "locomo-bus",
  867. .match = locomo_match,
  868. .suspend = locomo_bus_suspend,
  869. .resume = locomo_bus_resume,
  870. };
  871. int locomo_driver_register(struct locomo_driver *driver)
  872. {
  873. driver->drv.probe = locomo_bus_probe;
  874. driver->drv.remove = locomo_bus_remove;
  875. driver->drv.bus = &locomo_bus_type;
  876. return driver_register(&driver->drv);
  877. }
  878. void locomo_driver_unregister(struct locomo_driver *driver)
  879. {
  880. driver_unregister(&driver->drv);
  881. }
  882. static int __init locomo_init(void)
  883. {
  884. int ret = bus_register(&locomo_bus_type);
  885. if (ret == 0)
  886. driver_register(&locomo_device_driver);
  887. return ret;
  888. }
  889. static void __exit locomo_exit(void)
  890. {
  891. driver_unregister(&locomo_device_driver);
  892. bus_unregister(&locomo_bus_type);
  893. }
  894. module_init(locomo_init);
  895. module_exit(locomo_exit);
  896. MODULE_DESCRIPTION("Sharp LoCoMo core driver");
  897. MODULE_LICENSE("GPL");
  898. MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
  899. EXPORT_SYMBOL(locomo_driver_register);
  900. EXPORT_SYMBOL(locomo_driver_unregister);
  901. EXPORT_SYMBOL(locomo_gpio_set_dir);
  902. EXPORT_SYMBOL(locomo_gpio_read_level);
  903. EXPORT_SYMBOL(locomo_gpio_read_output);
  904. EXPORT_SYMBOL(locomo_gpio_write);
  905. EXPORT_SYMBOL(locomo_m62332_senddata);