locomo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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/module.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/io.h>
  27. #include <mach/hardware.h>
  28. #include <asm/irq.h>
  29. #include <asm/mach/irq.h>
  30. #include <asm/hardware/locomo.h>
  31. /* LoCoMo Interrupts */
  32. #define IRQ_LOCOMO_KEY (0)
  33. #define IRQ_LOCOMO_GPIO (1)
  34. #define IRQ_LOCOMO_LT (2)
  35. #define IRQ_LOCOMO_SPI (3)
  36. /* M62332 output channel selection */
  37. #define M62332_EVR_CH 1 /* M62332 volume channel number */
  38. /* 0 : CH.1 , 1 : CH. 2 */
  39. /* DAC send data */
  40. #define M62332_SLAVE_ADDR 0x4e /* Slave address */
  41. #define M62332_W_BIT 0x00 /* W bit (0 only) */
  42. #define M62332_SUB_ADDR 0x00 /* Sub address */
  43. #define M62332_A_BIT 0x00 /* A bit (0 only) */
  44. /* DAC setup and hold times (expressed in us) */
  45. #define DAC_BUS_FREE_TIME 5 /* 4.7 us */
  46. #define DAC_START_SETUP_TIME 5 /* 4.7 us */
  47. #define DAC_STOP_SETUP_TIME 4 /* 4.0 us */
  48. #define DAC_START_HOLD_TIME 5 /* 4.7 us */
  49. #define DAC_SCL_LOW_HOLD_TIME 5 /* 4.7 us */
  50. #define DAC_SCL_HIGH_HOLD_TIME 4 /* 4.0 us */
  51. #define DAC_DATA_SETUP_TIME 1 /* 250 ns */
  52. #define DAC_DATA_HOLD_TIME 1 /* 300 ns */
  53. #define DAC_LOW_SETUP_TIME 1 /* 300 ns */
  54. #define DAC_HIGH_SETUP_TIME 1 /* 1000 ns */
  55. /* the following is the overall data for the locomo chip */
  56. struct locomo {
  57. struct device *dev;
  58. unsigned long phys;
  59. unsigned int irq;
  60. int irq_base;
  61. spinlock_t lock;
  62. void __iomem *base;
  63. #ifdef CONFIG_PM
  64. void *saved_state;
  65. #endif
  66. };
  67. struct locomo_dev_info {
  68. unsigned long offset;
  69. unsigned long length;
  70. unsigned int devid;
  71. unsigned int irq[1];
  72. const char * name;
  73. };
  74. /* All the locomo devices. If offset is non-zero, the mapbase for the
  75. * locomo_dev will be set to the chip base plus offset. If offset is
  76. * zero, then the mapbase for the locomo_dev will be set to zero. An
  77. * offset of zero means the device only uses GPIOs or other helper
  78. * functions inside this file */
  79. static struct locomo_dev_info locomo_devices[] = {
  80. {
  81. .devid = LOCOMO_DEVID_KEYBOARD,
  82. .irq = { IRQ_LOCOMO_KEY },
  83. .name = "locomo-keyboard",
  84. .offset = LOCOMO_KEYBOARD,
  85. .length = 16,
  86. },
  87. {
  88. .devid = LOCOMO_DEVID_FRONTLIGHT,
  89. .irq = {},
  90. .name = "locomo-frontlight",
  91. .offset = LOCOMO_FRONTLIGHT,
  92. .length = 8,
  93. },
  94. {
  95. .devid = LOCOMO_DEVID_BACKLIGHT,
  96. .irq = {},
  97. .name = "locomo-backlight",
  98. .offset = LOCOMO_BACKLIGHT,
  99. .length = 8,
  100. },
  101. {
  102. .devid = LOCOMO_DEVID_AUDIO,
  103. .irq = {},
  104. .name = "locomo-audio",
  105. .offset = LOCOMO_AUDIO,
  106. .length = 4,
  107. },
  108. {
  109. .devid = LOCOMO_DEVID_LED,
  110. .irq = {},
  111. .name = "locomo-led",
  112. .offset = LOCOMO_LED,
  113. .length = 8,
  114. },
  115. {
  116. .devid = LOCOMO_DEVID_UART,
  117. .irq = {},
  118. .name = "locomo-uart",
  119. .offset = 0,
  120. .length = 0,
  121. },
  122. {
  123. .devid = LOCOMO_DEVID_SPI,
  124. .irq = {},
  125. .name = "locomo-spi",
  126. .offset = LOCOMO_SPI,
  127. .length = 0x30,
  128. },
  129. };
  130. static void locomo_handler(struct irq_desc *desc)
  131. {
  132. struct locomo *lchip = irq_desc_get_handler_data(desc);
  133. int req, i;
  134. /* Acknowledge the parent IRQ */
  135. desc->irq_data.chip->irq_ack(&desc->irq_data);
  136. /* check why this interrupt was generated */
  137. req = locomo_readl(lchip->base + LOCOMO_ICR) & 0x0f00;
  138. if (req) {
  139. unsigned int irq;
  140. /* generate the next interrupt(s) */
  141. irq = lchip->irq_base;
  142. for (i = 0; i <= 3; i++, irq++) {
  143. if (req & (0x0100 << i)) {
  144. generic_handle_irq(irq);
  145. }
  146. }
  147. }
  148. }
  149. static void locomo_ack_irq(struct irq_data *d)
  150. {
  151. }
  152. static void locomo_mask_irq(struct irq_data *d)
  153. {
  154. struct locomo *lchip = irq_data_get_irq_chip_data(d);
  155. unsigned int r;
  156. r = locomo_readl(lchip->base + LOCOMO_ICR);
  157. r &= ~(0x0010 << (d->irq - lchip->irq_base));
  158. locomo_writel(r, lchip->base + LOCOMO_ICR);
  159. }
  160. static void locomo_unmask_irq(struct irq_data *d)
  161. {
  162. struct locomo *lchip = irq_data_get_irq_chip_data(d);
  163. unsigned int r;
  164. r = locomo_readl(lchip->base + LOCOMO_ICR);
  165. r |= (0x0010 << (d->irq - lchip->irq_base));
  166. locomo_writel(r, lchip->base + LOCOMO_ICR);
  167. }
  168. static struct irq_chip locomo_chip = {
  169. .name = "LOCOMO",
  170. .irq_ack = locomo_ack_irq,
  171. .irq_mask = locomo_mask_irq,
  172. .irq_unmask = locomo_unmask_irq,
  173. };
  174. static void locomo_setup_irq(struct locomo *lchip)
  175. {
  176. int irq = lchip->irq_base;
  177. /*
  178. * Install handler for IRQ_LOCOMO_HW.
  179. */
  180. irq_set_irq_type(lchip->irq, IRQ_TYPE_EDGE_FALLING);
  181. irq_set_chained_handler_and_data(lchip->irq, locomo_handler, lchip);
  182. /* Install handlers for IRQ_LOCOMO_* */
  183. for ( ; irq <= lchip->irq_base + 3; irq++) {
  184. irq_set_chip_and_handler(irq, &locomo_chip, handle_level_irq);
  185. irq_set_chip_data(irq, lchip);
  186. irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
  187. }
  188. }
  189. static void locomo_dev_release(struct device *_dev)
  190. {
  191. struct locomo_dev *dev = LOCOMO_DEV(_dev);
  192. kfree(dev);
  193. }
  194. static int
  195. locomo_init_one_child(struct locomo *lchip, struct locomo_dev_info *info)
  196. {
  197. struct locomo_dev *dev;
  198. int ret;
  199. dev = kzalloc(sizeof(struct locomo_dev), GFP_KERNEL);
  200. if (!dev) {
  201. ret = -ENOMEM;
  202. goto out;
  203. }
  204. /*
  205. * If the parent device has a DMA mask associated with it,
  206. * propagate it down to the children.
  207. */
  208. if (lchip->dev->dma_mask) {
  209. dev->dma_mask = *lchip->dev->dma_mask;
  210. dev->dev.dma_mask = &dev->dma_mask;
  211. }
  212. dev_set_name(&dev->dev, "%s", info->name);
  213. dev->devid = info->devid;
  214. dev->dev.parent = lchip->dev;
  215. dev->dev.bus = &locomo_bus_type;
  216. dev->dev.release = locomo_dev_release;
  217. dev->dev.coherent_dma_mask = lchip->dev->coherent_dma_mask;
  218. if (info->offset)
  219. dev->mapbase = lchip->base + info->offset;
  220. else
  221. dev->mapbase = 0;
  222. dev->length = info->length;
  223. dev->irq[0] = (lchip->irq_base == NO_IRQ) ?
  224. NO_IRQ : lchip->irq_base + info->irq[0];
  225. ret = device_register(&dev->dev);
  226. if (ret) {
  227. out:
  228. kfree(dev);
  229. }
  230. return ret;
  231. }
  232. #ifdef CONFIG_PM
  233. struct locomo_save_data {
  234. u16 LCM_GPO;
  235. u16 LCM_SPICT;
  236. u16 LCM_GPE;
  237. u16 LCM_ASD;
  238. u16 LCM_SPIMD;
  239. };
  240. static int locomo_suspend(struct platform_device *dev, pm_message_t state)
  241. {
  242. struct locomo *lchip = platform_get_drvdata(dev);
  243. struct locomo_save_data *save;
  244. unsigned long flags;
  245. save = kmalloc(sizeof(struct locomo_save_data), GFP_KERNEL);
  246. if (!save)
  247. return -ENOMEM;
  248. lchip->saved_state = save;
  249. spin_lock_irqsave(&lchip->lock, flags);
  250. save->LCM_GPO = locomo_readl(lchip->base + LOCOMO_GPO); /* GPIO */
  251. locomo_writel(0x00, lchip->base + LOCOMO_GPO);
  252. save->LCM_SPICT = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPICT); /* SPI */
  253. locomo_writel(0x40, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
  254. save->LCM_GPE = locomo_readl(lchip->base + LOCOMO_GPE); /* GPIO */
  255. locomo_writel(0x00, lchip->base + LOCOMO_GPE);
  256. save->LCM_ASD = locomo_readl(lchip->base + LOCOMO_ASD); /* ADSTART */
  257. locomo_writel(0x00, lchip->base + LOCOMO_ASD);
  258. save->LCM_SPIMD = locomo_readl(lchip->base + LOCOMO_SPI + LOCOMO_SPIMD); /* SPI */
  259. locomo_writel(0x3C14, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
  260. locomo_writel(0x00, lchip->base + LOCOMO_PAIF);
  261. locomo_writel(0x00, lchip->base + LOCOMO_DAC);
  262. locomo_writel(0x00, lchip->base + LOCOMO_BACKLIGHT + LOCOMO_TC);
  263. if ((locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT0) & 0x88) && (locomo_readl(lchip->base + LOCOMO_LED + LOCOMO_LPT1) & 0x88))
  264. locomo_writel(0x00, lchip->base + LOCOMO_C32K); /* CLK32 off */
  265. else
  266. /* 18MHz already enabled, so no wait */
  267. locomo_writel(0xc1, lchip->base + LOCOMO_C32K); /* CLK32 on */
  268. locomo_writel(0x00, lchip->base + LOCOMO_TADC); /* 18MHz clock off*/
  269. locomo_writel(0x00, lchip->base + LOCOMO_AUDIO + LOCOMO_ACC); /* 22MHz/24MHz clock off */
  270. locomo_writel(0x00, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS); /* FL */
  271. spin_unlock_irqrestore(&lchip->lock, flags);
  272. return 0;
  273. }
  274. static int locomo_resume(struct platform_device *dev)
  275. {
  276. struct locomo *lchip = platform_get_drvdata(dev);
  277. struct locomo_save_data *save;
  278. unsigned long r;
  279. unsigned long flags;
  280. save = lchip->saved_state;
  281. if (!save)
  282. return 0;
  283. spin_lock_irqsave(&lchip->lock, flags);
  284. locomo_writel(save->LCM_GPO, lchip->base + LOCOMO_GPO);
  285. locomo_writel(save->LCM_SPICT, lchip->base + LOCOMO_SPI + LOCOMO_SPICT);
  286. locomo_writel(save->LCM_GPE, lchip->base + LOCOMO_GPE);
  287. locomo_writel(save->LCM_ASD, lchip->base + LOCOMO_ASD);
  288. locomo_writel(save->LCM_SPIMD, lchip->base + LOCOMO_SPI + LOCOMO_SPIMD);
  289. locomo_writel(0x00, lchip->base + LOCOMO_C32K);
  290. locomo_writel(0x90, lchip->base + LOCOMO_TADC);
  291. locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KSC);
  292. r = locomo_readl(lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
  293. r &= 0xFEFF;
  294. locomo_writel(r, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
  295. locomo_writel(0x1, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KCMD);
  296. spin_unlock_irqrestore(&lchip->lock, flags);
  297. lchip->saved_state = NULL;
  298. kfree(save);
  299. return 0;
  300. }
  301. #endif
  302. /**
  303. * locomo_probe - probe for a single LoCoMo chip.
  304. * @phys_addr: physical address of device.
  305. *
  306. * Probe for a LoCoMo chip. This must be called
  307. * before any other locomo-specific code.
  308. *
  309. * Returns:
  310. * %-ENODEV device not found.
  311. * %-EBUSY physical address already marked in-use.
  312. * %0 successful.
  313. */
  314. static int
  315. __locomo_probe(struct device *me, struct resource *mem, int irq)
  316. {
  317. struct locomo_platform_data *pdata = me->platform_data;
  318. struct locomo *lchip;
  319. unsigned long r;
  320. int i, ret = -ENODEV;
  321. lchip = kzalloc(sizeof(struct locomo), GFP_KERNEL);
  322. if (!lchip)
  323. return -ENOMEM;
  324. spin_lock_init(&lchip->lock);
  325. lchip->dev = me;
  326. dev_set_drvdata(lchip->dev, lchip);
  327. lchip->phys = mem->start;
  328. lchip->irq = irq;
  329. lchip->irq_base = (pdata) ? pdata->irq_base : NO_IRQ;
  330. /*
  331. * Map the whole region. This also maps the
  332. * registers for our children.
  333. */
  334. lchip->base = ioremap(mem->start, PAGE_SIZE);
  335. if (!lchip->base) {
  336. ret = -ENOMEM;
  337. goto out;
  338. }
  339. /* locomo initialize */
  340. locomo_writel(0, lchip->base + LOCOMO_ICR);
  341. /* KEYBOARD */
  342. locomo_writel(0, lchip->base + LOCOMO_KEYBOARD + LOCOMO_KIC);
  343. /* GPIO */
  344. locomo_writel(0, lchip->base + LOCOMO_GPO);
  345. locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
  346. , lchip->base + LOCOMO_GPE);
  347. locomo_writel((LOCOMO_GPIO(1) | LOCOMO_GPIO(2) | LOCOMO_GPIO(13) | LOCOMO_GPIO(14))
  348. , lchip->base + LOCOMO_GPD);
  349. locomo_writel(0, lchip->base + LOCOMO_GIE);
  350. /* Frontlight */
  351. locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
  352. locomo_writel(0, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
  353. /* Longtime timer */
  354. locomo_writel(0, lchip->base + LOCOMO_LTINT);
  355. /* SPI */
  356. locomo_writel(0, lchip->base + LOCOMO_SPI + LOCOMO_SPIIE);
  357. locomo_writel(6 + 8 + 320 + 30 - 10, lchip->base + LOCOMO_ASD);
  358. r = locomo_readl(lchip->base + LOCOMO_ASD);
  359. r |= 0x8000;
  360. locomo_writel(r, lchip->base + LOCOMO_ASD);
  361. locomo_writel(6 + 8 + 320 + 30 - 10 - 128 + 4, lchip->base + LOCOMO_HSD);
  362. r = locomo_readl(lchip->base + LOCOMO_HSD);
  363. r |= 0x8000;
  364. locomo_writel(r, lchip->base + LOCOMO_HSD);
  365. locomo_writel(128 / 8, lchip->base + LOCOMO_HSC);
  366. /* XON */
  367. locomo_writel(0x80, lchip->base + LOCOMO_TADC);
  368. udelay(1000);
  369. /* CLK9MEN */
  370. r = locomo_readl(lchip->base + LOCOMO_TADC);
  371. r |= 0x10;
  372. locomo_writel(r, lchip->base + LOCOMO_TADC);
  373. udelay(100);
  374. /* init DAC */
  375. r = locomo_readl(lchip->base + LOCOMO_DAC);
  376. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  377. locomo_writel(r, lchip->base + LOCOMO_DAC);
  378. r = locomo_readl(lchip->base + LOCOMO_VER);
  379. printk(KERN_INFO "LoCoMo Chip: %lu%lu\n", (r >> 8), (r & 0xff));
  380. /*
  381. * The interrupt controller must be initialised before any
  382. * other device to ensure that the interrupts are available.
  383. */
  384. if (lchip->irq != NO_IRQ && lchip->irq_base != NO_IRQ)
  385. locomo_setup_irq(lchip);
  386. for (i = 0; i < ARRAY_SIZE(locomo_devices); i++)
  387. locomo_init_one_child(lchip, &locomo_devices[i]);
  388. return 0;
  389. out:
  390. kfree(lchip);
  391. return ret;
  392. }
  393. static int locomo_remove_child(struct device *dev, void *data)
  394. {
  395. device_unregister(dev);
  396. return 0;
  397. }
  398. static void __locomo_remove(struct locomo *lchip)
  399. {
  400. device_for_each_child(lchip->dev, NULL, locomo_remove_child);
  401. if (lchip->irq != NO_IRQ) {
  402. irq_set_chained_handler_and_data(lchip->irq, NULL, NULL);
  403. }
  404. iounmap(lchip->base);
  405. kfree(lchip);
  406. }
  407. static int locomo_probe(struct platform_device *dev)
  408. {
  409. struct resource *mem;
  410. int irq;
  411. mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
  412. if (!mem)
  413. return -EINVAL;
  414. irq = platform_get_irq(dev, 0);
  415. if (irq < 0)
  416. return -ENXIO;
  417. return __locomo_probe(&dev->dev, mem, irq);
  418. }
  419. static int locomo_remove(struct platform_device *dev)
  420. {
  421. struct locomo *lchip = platform_get_drvdata(dev);
  422. if (lchip) {
  423. __locomo_remove(lchip);
  424. platform_set_drvdata(dev, NULL);
  425. }
  426. return 0;
  427. }
  428. /*
  429. * Not sure if this should be on the system bus or not yet.
  430. * We really want some way to register a system device at
  431. * the per-machine level, and then have this driver pick
  432. * up the registered devices.
  433. */
  434. static struct platform_driver locomo_device_driver = {
  435. .probe = locomo_probe,
  436. .remove = locomo_remove,
  437. #ifdef CONFIG_PM
  438. .suspend = locomo_suspend,
  439. .resume = locomo_resume,
  440. #endif
  441. .driver = {
  442. .name = "locomo",
  443. },
  444. };
  445. /*
  446. * Get the parent device driver (us) structure
  447. * from a child function device
  448. */
  449. static inline struct locomo *locomo_chip_driver(struct locomo_dev *ldev)
  450. {
  451. return (struct locomo *)dev_get_drvdata(ldev->dev.parent);
  452. }
  453. void locomo_gpio_set_dir(struct device *dev, unsigned int bits, unsigned int dir)
  454. {
  455. struct locomo *lchip = dev_get_drvdata(dev);
  456. unsigned long flags;
  457. unsigned int r;
  458. if (!lchip)
  459. return;
  460. spin_lock_irqsave(&lchip->lock, flags);
  461. r = locomo_readl(lchip->base + LOCOMO_GPD);
  462. if (dir)
  463. r |= bits;
  464. else
  465. r &= ~bits;
  466. locomo_writel(r, lchip->base + LOCOMO_GPD);
  467. r = locomo_readl(lchip->base + LOCOMO_GPE);
  468. if (dir)
  469. r |= bits;
  470. else
  471. r &= ~bits;
  472. locomo_writel(r, lchip->base + LOCOMO_GPE);
  473. spin_unlock_irqrestore(&lchip->lock, flags);
  474. }
  475. EXPORT_SYMBOL(locomo_gpio_set_dir);
  476. int locomo_gpio_read_level(struct device *dev, unsigned int bits)
  477. {
  478. struct locomo *lchip = dev_get_drvdata(dev);
  479. unsigned long flags;
  480. unsigned int ret;
  481. if (!lchip)
  482. return -ENODEV;
  483. spin_lock_irqsave(&lchip->lock, flags);
  484. ret = locomo_readl(lchip->base + LOCOMO_GPL);
  485. spin_unlock_irqrestore(&lchip->lock, flags);
  486. ret &= bits;
  487. return ret;
  488. }
  489. EXPORT_SYMBOL(locomo_gpio_read_level);
  490. int locomo_gpio_read_output(struct device *dev, unsigned int bits)
  491. {
  492. struct locomo *lchip = dev_get_drvdata(dev);
  493. unsigned long flags;
  494. unsigned int ret;
  495. if (!lchip)
  496. return -ENODEV;
  497. spin_lock_irqsave(&lchip->lock, flags);
  498. ret = locomo_readl(lchip->base + LOCOMO_GPO);
  499. spin_unlock_irqrestore(&lchip->lock, flags);
  500. ret &= bits;
  501. return ret;
  502. }
  503. EXPORT_SYMBOL(locomo_gpio_read_output);
  504. void locomo_gpio_write(struct device *dev, unsigned int bits, unsigned int set)
  505. {
  506. struct locomo *lchip = dev_get_drvdata(dev);
  507. unsigned long flags;
  508. unsigned int r;
  509. if (!lchip)
  510. return;
  511. spin_lock_irqsave(&lchip->lock, flags);
  512. r = locomo_readl(lchip->base + LOCOMO_GPO);
  513. if (set)
  514. r |= bits;
  515. else
  516. r &= ~bits;
  517. locomo_writel(r, lchip->base + LOCOMO_GPO);
  518. spin_unlock_irqrestore(&lchip->lock, flags);
  519. }
  520. EXPORT_SYMBOL(locomo_gpio_write);
  521. static void locomo_m62332_sendbit(void *mapbase, int bit)
  522. {
  523. unsigned int r;
  524. r = locomo_readl(mapbase + LOCOMO_DAC);
  525. r &= ~(LOCOMO_DAC_SCLOEB);
  526. locomo_writel(r, mapbase + LOCOMO_DAC);
  527. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  528. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  529. r = locomo_readl(mapbase + LOCOMO_DAC);
  530. r &= ~(LOCOMO_DAC_SCLOEB);
  531. locomo_writel(r, mapbase + LOCOMO_DAC);
  532. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  533. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  534. if (bit & 1) {
  535. r = locomo_readl(mapbase + LOCOMO_DAC);
  536. r |= LOCOMO_DAC_SDAOEB;
  537. locomo_writel(r, mapbase + LOCOMO_DAC);
  538. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  539. } else {
  540. r = locomo_readl(mapbase + LOCOMO_DAC);
  541. r &= ~(LOCOMO_DAC_SDAOEB);
  542. locomo_writel(r, mapbase + LOCOMO_DAC);
  543. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  544. }
  545. udelay(DAC_DATA_SETUP_TIME); /* 250 nsec */
  546. r = locomo_readl(mapbase + LOCOMO_DAC);
  547. r |= LOCOMO_DAC_SCLOEB;
  548. locomo_writel(r, mapbase + LOCOMO_DAC);
  549. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  550. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  551. }
  552. void locomo_m62332_senddata(struct locomo_dev *ldev, unsigned int dac_data, int channel)
  553. {
  554. struct locomo *lchip = locomo_chip_driver(ldev);
  555. int i;
  556. unsigned char data;
  557. unsigned int r;
  558. void *mapbase = lchip->base;
  559. unsigned long flags;
  560. spin_lock_irqsave(&lchip->lock, flags);
  561. /* Start */
  562. udelay(DAC_BUS_FREE_TIME); /* 5.0 usec */
  563. r = locomo_readl(mapbase + LOCOMO_DAC);
  564. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  565. locomo_writel(r, mapbase + LOCOMO_DAC);
  566. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  567. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.0 usec */
  568. r = locomo_readl(mapbase + LOCOMO_DAC);
  569. r &= ~(LOCOMO_DAC_SDAOEB);
  570. locomo_writel(r, mapbase + LOCOMO_DAC);
  571. udelay(DAC_START_HOLD_TIME); /* 5.0 usec */
  572. udelay(DAC_DATA_HOLD_TIME); /* 300 nsec */
  573. /* Send slave address and W bit (LSB is W bit) */
  574. data = (M62332_SLAVE_ADDR << 1) | M62332_W_BIT;
  575. for (i = 1; i <= 8; i++) {
  576. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  577. }
  578. /* Check A bit */
  579. r = locomo_readl(mapbase + LOCOMO_DAC);
  580. r &= ~(LOCOMO_DAC_SCLOEB);
  581. locomo_writel(r, mapbase + LOCOMO_DAC);
  582. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  583. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  584. r = locomo_readl(mapbase + LOCOMO_DAC);
  585. r &= ~(LOCOMO_DAC_SDAOEB);
  586. locomo_writel(r, mapbase + LOCOMO_DAC);
  587. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  588. r = locomo_readl(mapbase + LOCOMO_DAC);
  589. r |= LOCOMO_DAC_SCLOEB;
  590. locomo_writel(r, mapbase + LOCOMO_DAC);
  591. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  592. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  593. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  594. printk(KERN_WARNING "locomo: m62332_senddata Error 1\n");
  595. goto out;
  596. }
  597. /* Send Sub address (LSB is channel select) */
  598. /* channel = 0 : ch1 select */
  599. /* = 1 : ch2 select */
  600. data = M62332_SUB_ADDR + channel;
  601. for (i = 1; i <= 8; i++) {
  602. locomo_m62332_sendbit(mapbase, data >> (8 - i));
  603. }
  604. /* Check A bit */
  605. r = locomo_readl(mapbase + LOCOMO_DAC);
  606. r &= ~(LOCOMO_DAC_SCLOEB);
  607. locomo_writel(r, mapbase + LOCOMO_DAC);
  608. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  609. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  610. r = locomo_readl(mapbase + LOCOMO_DAC);
  611. r &= ~(LOCOMO_DAC_SDAOEB);
  612. locomo_writel(r, mapbase + LOCOMO_DAC);
  613. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  614. r = locomo_readl(mapbase + LOCOMO_DAC);
  615. r |= LOCOMO_DAC_SCLOEB;
  616. locomo_writel(r, mapbase + LOCOMO_DAC);
  617. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  618. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  619. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  620. printk(KERN_WARNING "locomo: m62332_senddata Error 2\n");
  621. goto out;
  622. }
  623. /* Send DAC data */
  624. for (i = 1; i <= 8; i++) {
  625. locomo_m62332_sendbit(mapbase, dac_data >> (8 - i));
  626. }
  627. /* Check A bit */
  628. r = locomo_readl(mapbase + LOCOMO_DAC);
  629. r &= ~(LOCOMO_DAC_SCLOEB);
  630. locomo_writel(r, mapbase + LOCOMO_DAC);
  631. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  632. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  633. r = locomo_readl(mapbase + LOCOMO_DAC);
  634. r &= ~(LOCOMO_DAC_SDAOEB);
  635. locomo_writel(r, mapbase + LOCOMO_DAC);
  636. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  637. r = locomo_readl(mapbase + LOCOMO_DAC);
  638. r |= LOCOMO_DAC_SCLOEB;
  639. locomo_writel(r, mapbase + LOCOMO_DAC);
  640. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  641. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4.7 usec */
  642. if (locomo_readl(mapbase + LOCOMO_DAC) & LOCOMO_DAC_SDAOEB) { /* High is error */
  643. printk(KERN_WARNING "locomo: m62332_senddata Error 3\n");
  644. }
  645. out:
  646. /* stop */
  647. r = locomo_readl(mapbase + LOCOMO_DAC);
  648. r &= ~(LOCOMO_DAC_SCLOEB);
  649. locomo_writel(r, mapbase + LOCOMO_DAC);
  650. udelay(DAC_LOW_SETUP_TIME); /* 300 nsec */
  651. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  652. r = locomo_readl(mapbase + LOCOMO_DAC);
  653. r |= LOCOMO_DAC_SCLOEB;
  654. locomo_writel(r, mapbase + LOCOMO_DAC);
  655. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  656. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  657. r = locomo_readl(mapbase + LOCOMO_DAC);
  658. r |= LOCOMO_DAC_SDAOEB;
  659. locomo_writel(r, mapbase + LOCOMO_DAC);
  660. udelay(DAC_HIGH_SETUP_TIME); /* 1000 nsec */
  661. udelay(DAC_SCL_HIGH_HOLD_TIME); /* 4 usec */
  662. r = locomo_readl(mapbase + LOCOMO_DAC);
  663. r |= LOCOMO_DAC_SCLOEB | LOCOMO_DAC_SDAOEB;
  664. locomo_writel(r, mapbase + LOCOMO_DAC);
  665. udelay(DAC_LOW_SETUP_TIME); /* 1000 nsec */
  666. udelay(DAC_SCL_LOW_HOLD_TIME); /* 4.7 usec */
  667. spin_unlock_irqrestore(&lchip->lock, flags);
  668. }
  669. EXPORT_SYMBOL(locomo_m62332_senddata);
  670. /*
  671. * Frontlight control
  672. */
  673. void locomo_frontlight_set(struct locomo_dev *dev, int duty, int vr, int bpwf)
  674. {
  675. unsigned long flags;
  676. struct locomo *lchip = locomo_chip_driver(dev);
  677. if (vr)
  678. locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 1);
  679. else
  680. locomo_gpio_write(dev->dev.parent, LOCOMO_GPIO_FL_VR, 0);
  681. spin_lock_irqsave(&lchip->lock, flags);
  682. locomo_writel(bpwf, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
  683. udelay(100);
  684. locomo_writel(duty, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALD);
  685. locomo_writel(bpwf | LOCOMO_ALC_EN, lchip->base + LOCOMO_FRONTLIGHT + LOCOMO_ALS);
  686. spin_unlock_irqrestore(&lchip->lock, flags);
  687. }
  688. EXPORT_SYMBOL(locomo_frontlight_set);
  689. /*
  690. * LoCoMo "Register Access Bus."
  691. *
  692. * We model this as a regular bus type, and hang devices directly
  693. * off this.
  694. */
  695. static int locomo_match(struct device *_dev, struct device_driver *_drv)
  696. {
  697. struct locomo_dev *dev = LOCOMO_DEV(_dev);
  698. struct locomo_driver *drv = LOCOMO_DRV(_drv);
  699. return dev->devid == drv->devid;
  700. }
  701. static int locomo_bus_suspend(struct device *dev, pm_message_t state)
  702. {
  703. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  704. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  705. int ret = 0;
  706. if (drv && drv->suspend)
  707. ret = drv->suspend(ldev, state);
  708. return ret;
  709. }
  710. static int locomo_bus_resume(struct device *dev)
  711. {
  712. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  713. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  714. int ret = 0;
  715. if (drv && drv->resume)
  716. ret = drv->resume(ldev);
  717. return ret;
  718. }
  719. static int locomo_bus_probe(struct device *dev)
  720. {
  721. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  722. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  723. int ret = -ENODEV;
  724. if (drv->probe)
  725. ret = drv->probe(ldev);
  726. return ret;
  727. }
  728. static int locomo_bus_remove(struct device *dev)
  729. {
  730. struct locomo_dev *ldev = LOCOMO_DEV(dev);
  731. struct locomo_driver *drv = LOCOMO_DRV(dev->driver);
  732. int ret = 0;
  733. if (drv->remove)
  734. ret = drv->remove(ldev);
  735. return ret;
  736. }
  737. struct bus_type locomo_bus_type = {
  738. .name = "locomo-bus",
  739. .match = locomo_match,
  740. .probe = locomo_bus_probe,
  741. .remove = locomo_bus_remove,
  742. .suspend = locomo_bus_suspend,
  743. .resume = locomo_bus_resume,
  744. };
  745. int locomo_driver_register(struct locomo_driver *driver)
  746. {
  747. driver->drv.bus = &locomo_bus_type;
  748. return driver_register(&driver->drv);
  749. }
  750. EXPORT_SYMBOL(locomo_driver_register);
  751. void locomo_driver_unregister(struct locomo_driver *driver)
  752. {
  753. driver_unregister(&driver->drv);
  754. }
  755. EXPORT_SYMBOL(locomo_driver_unregister);
  756. static int __init locomo_init(void)
  757. {
  758. int ret = bus_register(&locomo_bus_type);
  759. if (ret == 0)
  760. platform_driver_register(&locomo_device_driver);
  761. return ret;
  762. }
  763. static void __exit locomo_exit(void)
  764. {
  765. platform_driver_unregister(&locomo_device_driver);
  766. bus_unregister(&locomo_bus_type);
  767. }
  768. module_init(locomo_init);
  769. module_exit(locomo_exit);
  770. MODULE_DESCRIPTION("Sharp LoCoMo core driver");
  771. MODULE_LICENSE("GPL");
  772. MODULE_AUTHOR("John Lenz <lenz@cs.wisc.edu>");