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. desc_handle_irq(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. desc_handle_irq(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. desc_handle_irq(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. desc_handle_irq(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. desc_handle_irq(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 int locomo_remove_child(struct device *dev, void *data)
  563. {
  564. device_unregister(dev);
  565. return 0;
  566. }
  567. static void __locomo_remove(struct locomo *lchip)
  568. {
  569. device_for_each_child(lchip->dev, NULL, locomo_remove_child);
  570. if (lchip->irq != NO_IRQ) {
  571. set_irq_chained_handler(lchip->irq, NULL);
  572. set_irq_data(lchip->irq, NULL);
  573. }
  574. iounmap(lchip->base);
  575. kfree(lchip);
  576. }
  577. static int locomo_probe(struct device *dev)
  578. {
  579. struct platform_device *pdev = to_platform_device(dev);
  580. struct resource *mem;
  581. int irq;
  582. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  583. if (!mem)
  584. return -EINVAL;
  585. irq = platform_get_irq(pdev, 0);
  586. return __locomo_probe(dev, mem, irq);
  587. }
  588. static int locomo_remove(struct device *dev)
  589. {
  590. struct locomo *lchip = dev_get_drvdata(dev);
  591. if (lchip) {
  592. __locomo_remove(lchip);
  593. dev_set_drvdata(dev, NULL);
  594. }
  595. return 0;
  596. }
  597. /*
  598. * Not sure if this should be on the system bus or not yet.
  599. * We really want some way to register a system device at
  600. * the per-machine level, and then have this driver pick
  601. * up the registered devices.
  602. */
  603. static struct device_driver locomo_device_driver = {
  604. .name = "locomo",
  605. .bus = &platform_bus_type,
  606. .probe = locomo_probe,
  607. .remove = locomo_remove,
  608. };
  609. /*
  610. * Get the parent device driver (us) structure
  611. * from a child function device
  612. */
  613. static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
  614. {
  615. return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
  616. }
  617. void locomo_gpio_set_dir(struct locomo_dev *ldev, unsigned int bits, unsigned int dir)
  618. {
  619. struct locomo *lchip = locomo_chip_driver(ldev);
  620. unsigned long flags;
  621. unsigned int r;
  622. spin_lock_irqsave(&lchip->lock, flags);
  623. r = locomo_readl(lchip->base + LOCOMO_GPD);
  624. r &= ~bits;
  625. locomo_writel(r, lchip->base + LOCOMO_GPD);
  626. r = locomo_readl(lchip->base + LOCOMO_GPE);
  627. if (dir)
  628. r |= bits;
  629. else
  630. r &= ~bits;
  631. locomo_writel(r, lchip->base + LOCOMO_GPE);
  632. spin_unlock_irqrestore(&lchip->lock, flags);
  633. }
  634. unsigned int locomo_gpio_read_level(struct locomo_dev *ldev, unsigned int bits)
  635. {
  636. struct locomo *lchip = locomo_chip_driver(ldev);
  637. unsigned long flags;
  638. unsigned int ret;
  639. spin_lock_irqsave(&lchip->lock, flags);
  640. ret = locomo_readl(lchip->base + LOCOMO_GPL);
  641. spin_unlock_irqrestore(&lchip->lock, flags);
  642. ret &= bits;
  643. return ret;
  644. }
  645. unsigned int locomo_gpio_read_output(struct locomo_dev *ldev, unsigned int bits)
  646. {
  647. struct locomo *lchip = locomo_chip_driver(ldev);
  648. unsigned long flags;
  649. unsigned int ret;
  650. spin_lock_irqsave(&lchip->lock, flags);
  651. ret = locomo_readl(lchip->base + LOCOMO_GPO);
  652. spin_unlock_irqrestore(&lchip->lock, flags);
  653. ret &= bits;
  654. return ret;
  655. }
  656. void locomo_gpio_write(struct locomo_dev *ldev, unsigned int bits, unsigned int set)
  657. {
  658. struct locomo *lchip = locomo_chip_driver(ldev);
  659. unsigned long flags;
  660. unsigned int r;
  661. spin_lock_irqsave(&lchip->lock, flags);
  662. r = locomo_readl(lchip->base + LOCOMO_GPO);
  663. if (set)
  664. r |= bits;
  665. else
  666. r &= ~bits;
  667. locomo_writel(r, lchip->base + LOCOMO_GPO);
  668. spin_unlock_irqrestore(&lchip->lock, flags);
  669. }
  670. static void locomo_m62332_sendbit(void *mapbase, int bit)
  671. {
  672. unsigned int r;
  673. r = locomo_readl(mapbase + LOCOMO_DAC);
  674. r &= ~(LOCOMO_DAC_SCLOEB);
  675. locomo_writel(r, mapbase + LOCOMO_DAC);
  676. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  677. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  678. r = locomo_readl(mapbase + LOCOMO_DAC);
  679. r &= ~(LOCOMO_DAC_SCLOEB);
  680. locomo_writel(r, mapbase + LOCOMO_DAC);
  681. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  682. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  683. if (bit & 1) {
  684. r = locomo_readl(mapbase + LOCOMO_DAC);
  685. r |= LOCOMO_DAC_SDAOEB;
  686. locomo_writel(r, mapbase + LOCOMO_DAC);
  687. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  688. } else {
  689. r = locomo_readl(mapbase + LOCOMO_DAC);
  690. r &= ~(LOCOMO_DAC_SDAOEB);
  691. locomo_writel(r, mapbase + LOCOMO_DAC);
  692. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  693. }
  694. udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
  695. r = locomo_readl(mapbase + LOCOMO_DAC);
  696. r |= LOCOMO_DAC_SCLOEB;
  697. locomo_writel(r, mapbase + LOCOMO_DAC);
  698. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  699. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  700. }
  701. void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
  702. {
  703. struct locomo *lchip = locomo_chip_driver(ldev);
  704. int i;
  705. unsigned char data;
  706. unsigned int r;
  707. void *mapbase = lchip->base;
  708. unsigned long flags;
  709. spin_lock_irqsave(&lchip->lock, flags);
  710. /* Start */
  711. udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
  712. r = locomo_readl(mapbase + LOCOMO_DAC);
  713. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  714. locomo_writel(r, mapbase + LOCOMO_DAC);
  715. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  716. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  717. r = locomo_readl(mapbase + LOCOMO_DAC);
  718. r &= ~(LOCOMO_DAC_SDAOEB);
  719. locomo_writel(r, mapbase + LOCOMO_DAC);
  720. udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
  721. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  722. /* Send slave address and W bit (LSB is W bit) */
  723. data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
  724. for (i = 1; i <= 8; i++) {
  725. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  726. }
  727. /* Check A bit */
  728. r = locomo_readl(mapbase + LOCOMO_DAC);
  729. r &= ~(LOCOMO_DAC_SCLOEB);
  730. locomo_writel(r, mapbase + LOCOMO_DAC);
  731. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  732. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  733. r = locomo_readl(mapbase + LOCOMO_DAC);
  734. r &= ~(LOCOMO_DAC_SDAOEB);
  735. locomo_writel(r, mapbase + LOCOMO_DAC);
  736. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  737. r = locomo_readl(mapbase + LOCOMO_DAC);
  738. r |= LOCOMO_DAC_SCLOEB;
  739. locomo_writel(r, mapbase + LOCOMO_DAC);
  740. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  741. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  742. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  743. printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
  744. return;
  745. }
  746. /* Send Sub address (LSB is channel select) */
  747. /* channel = 0 : ch1 select */
  748. /* = 1 : ch2 select */
  749. data = M62332_SUB_ADDR + channel;
  750. for (i = 1; i <= 8; i++) {
  751. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  752. }
  753. /* Check A bit */
  754. r = locomo_readl(mapbase + LOCOMO_DAC);
  755. r &= ~(LOCOMO_DAC_SCLOEB);
  756. locomo_writel(r, mapbase + LOCOMO_DAC);
  757. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  758. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  759. r = locomo_readl(mapbase + LOCOMO_DAC);
  760. r &= ~(LOCOMO_DAC_SDAOEB);
  761. locomo_writel(r, mapbase + LOCOMO_DAC);
  762. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  763. r = locomo_readl(mapbase + LOCOMO_DAC);
  764. r |= LOCOMO_DAC_SCLOEB;
  765. locomo_writel(r, mapbase + LOCOMO_DAC);
  766. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  767. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  768. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  769. printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
  770. return;
  771. }
  772. /* Send DAC data */
  773. for (i = 1; i <= 8; i++) {
  774. locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
  775. }
  776. /* Check A bit */
  777. r = locomo_readl(mapbase + LOCOMO_DAC);
  778. r &= ~(LOCOMO_DAC_SCLOEB);
  779. locomo_writel(r, mapbase + LOCOMO_DAC);
  780. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  781. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  782. r = locomo_readl(mapbase + LOCOMO_DAC);
  783. r &= ~(LOCOMO_DAC_SDAOEB);
  784. locomo_writel(r, mapbase + LOCOMO_DAC);
  785. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  786. r = locomo_readl(mapbase + LOCOMO_DAC);
  787. r |= LOCOMO_DAC_SCLOEB;
  788. locomo_writel(r, mapbase + LOCOMO_DAC);
  789. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  790. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  791. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  792. printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
  793. return;
  794. }
  795. /* stop */
  796. r = locomo_readl(mapbase + LOCOMO_DAC);
  797. r &= ~(LOCOMO_DAC_SCLOEB);
  798. locomo_writel(r, mapbase + LOCOMO_DAC);
  799. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  800. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  801. r = locomo_readl(mapbase + LOCOMO_DAC);
  802. r |= LOCOMO_DAC_SCLOEB;
  803. locomo_writel(r, mapbase + LOCOMO_DAC);
  804. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  805. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  806. r = locomo_readl(mapbase + LOCOMO_DAC);
  807. r |= LOCOMO_DAC_SDAOEB;
  808. locomo_writel(r, mapbase + LOCOMO_DAC);
  809. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  810. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  811. r = locomo_readl(mapbase + LOCOMO_DAC);
  812. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  813. locomo_writel(r, mapbase + LOCOMO_DAC);
  814. udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
  815. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  816. spin_unlock_irqrestore(&lchip->lock, flags);
  817. }
  818. /*
  819. * LoCoMo "Register Access Bus."
  820. *
  821. * We model this as a regular bus type, and hang devices directly
  822. * off this.
  823. */
  824. static int locomo_match(struct device *_dev, struct device_driver *_drv)
  825. {
  826. struct locomo_dev *dev = LOCOMO_DEV(_dev);
  827. struct locomo_driver *drv = LOCOMO_DRV(_drv);
  828. return dev->devid == drv->devid;
  829. }
  830. static int locomo_bus_suspend(struct device *dev, pm_message_t state)
  831. {
  832. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  833. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  834. int ret = 0;
  835. if (drv && drv->suspend)
  836. ret = drv->suspend(ldev, state);
  837. return ret;
  838. }
  839. static int locomo_bus_resume(struct device *dev)
  840. {
  841. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  842. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  843. int ret = 0;
  844. if (drv && drv->resume)
  845. ret = drv->resume(ldev);
  846. return ret;
  847. }
  848. static int locomo_bus_probe(struct device *dev)
  849. {
  850. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  851. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  852. int ret = -ENODEV;
  853. if (drv->probe)
  854. ret = drv->probe(ldev);
  855. return ret;
  856. }
  857. static int locomo_bus_remove(struct device *dev)
  858. {
  859. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  860. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  861. int ret = 0;
  862. if (drv->remove)
  863. ret = drv->remove(ldev);
  864. return ret;
  865. }
  866. struct bus_type locomo_bus_type = {
  867. .name = "locomo-bus",
  868. .match = locomo_match,
  869. .suspend = locomo_bus_suspend,
  870. .resume = locomo_bus_resume,
  871. };
  872. int locomo_driver_register(struct locomo_driver *driver)
  873. {
  874. driver->drv.probe = locomo_bus_probe;
  875. driver->drv.remove = locomo_bus_remove;
  876. driver->drv.bus = &locomo_bus_type;
  877. return driver_register(&driver->drv);
  878. }
  879. void locomo_driver_unregister(struct locomo_driver *driver)
  880. {
  881. driver_unregister(&driver->drv);
  882. }
  883. static int __init locomo_init(void)
  884. {
  885. int ret = bus_register(&locomo_bus_type);
  886. if (ret == 0)
  887. driver_register(&locomo_device_driver);
  888. return ret;
  889. }
  890. static void __exit locomo_exit(void)
  891. {
  892. driver_unregister(&locomo_device_driver);
  893. bus_unregister(&locomo_bus_type);
  894. }
  895. module_init(locomo_init);
  896. module_exit(locomo_exit);
  897. MODULE_DESCRIPTION("Sharp LoCoMo core driver");
  898. MODULE_LICENSE("GPL");
  899. MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");
  900. EXPORT_SYMBOL(locomo_driver_register);
  901. EXPORT_SYMBOL(locomo_driver_unregister);
  902. EXPORT_SYMBOL(locomo_gpio_set_dir);
  903. EXPORT_SYMBOL(locomo_gpio_read_level);
  904. EXPORT_SYMBOL(locomo_gpio_read_output);
  905. EXPORT_SYMBOL(locomo_gpio_write);
  906. EXPORT_SYMBOL(locomo_m62332_senddata);