dm355evm_msp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * dm355evm_msp.c - driver for MSP430 firmware on DM355EVM board
  3. *
  4. * Copyright (C) 2008 David Brownell
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/mutex.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/leds.h>
  18. #include <linux/i2c.h>
  19. #include <linux/i2c/dm355evm_msp.h>
  20. /*
  21. * The DM355 is a DaVinci chip with video support but no C64+ DSP. Its
  22. * EVM board has an MSP430 programmed with firmware for various board
  23. * support functions. This driver exposes some of them directly, and
  24. * supports other drivers (e.g. RTC, input) for more complex access.
  25. *
  26. * Because this firmware is entirely board-specific, this file embeds
  27. * knowledge that would be passed as platform_data in a generic driver.
  28. *
  29. * This driver was tested with firmware revision A4.
  30. */
  31. #if defined(CONFIG_INPUT_DM355EVM) || defined(CONFIG_INPUT_DM355EVM_MODULE)
  32. #define msp_has_keyboard() true
  33. #else
  34. #define msp_has_keyboard() false
  35. #endif
  36. #if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  37. #define msp_has_leds() true
  38. #else
  39. #define msp_has_leds() false
  40. #endif
  41. #if defined(CONFIG_RTC_DRV_DM355EVM) || defined(CONFIG_RTC_DRV_DM355EVM_MODULE)
  42. #define msp_has_rtc() true
  43. #else
  44. #define msp_has_rtc() false
  45. #endif
  46. #if defined(CONFIG_VIDEO_TVP514X) || defined(CONFIG_VIDEO_TVP514X_MODULE)
  47. #define msp_has_tvp() true
  48. #else
  49. #define msp_has_tvp() false
  50. #endif
  51. /*----------------------------------------------------------------------*/
  52. /* REVISIT for paranoia's sake, retry reads/writes on error */
  53. static struct i2c_client *msp430;
  54. /**
  55. * dm355evm_msp_write - Writes a register in dm355evm_msp
  56. * @value: the value to be written
  57. * @reg: register address
  58. *
  59. * Returns result of operation - 0 is success, else negative errno
  60. */
  61. int dm355evm_msp_write(u8 value, u8 reg)
  62. {
  63. return i2c_smbus_write_byte_data(msp430, reg, value);
  64. }
  65. EXPORT_SYMBOL(dm355evm_msp_write);
  66. /**
  67. * dm355evm_msp_read - Reads a register from dm355evm_msp
  68. * @reg: register address
  69. *
  70. * Returns result of operation - value, or negative errno
  71. */
  72. int dm355evm_msp_read(u8 reg)
  73. {
  74. return i2c_smbus_read_byte_data(msp430, reg);
  75. }
  76. EXPORT_SYMBOL(dm355evm_msp_read);
  77. /*----------------------------------------------------------------------*/
  78. /*
  79. * Many of the msp430 pins are just used as fixed-direction GPIOs.
  80. * We could export a few more of them this way, if we wanted.
  81. */
  82. #define MSP_GPIO(bit,reg) ((DM355EVM_MSP_ ## reg) << 3 | (bit))
  83. static const u8 msp_gpios[] = {
  84. /* eight leds */
  85. MSP_GPIO(0, LED), MSP_GPIO(1, LED),
  86. MSP_GPIO(2, LED), MSP_GPIO(3, LED),
  87. MSP_GPIO(4, LED), MSP_GPIO(5, LED),
  88. MSP_GPIO(6, LED), MSP_GPIO(7, LED),
  89. /* SW6 and the NTSC/nPAL jumper */
  90. MSP_GPIO(0, SWITCH1), MSP_GPIO(1, SWITCH1),
  91. MSP_GPIO(2, SWITCH1), MSP_GPIO(3, SWITCH1),
  92. MSP_GPIO(4, SWITCH1),
  93. /* switches on MMC/SD sockets */
  94. /*
  95. * Note: EVMDM355_ECP_VA4.pdf suggests that Bit 2 and 4 should be
  96. * checked for card detection. However on the EVM bit 1 and 3 gives
  97. * this status, for 0 and 1 instance respectively. The pdf also
  98. * suggests that Bit 1 and 3 should be checked for write protection.
  99. * However on the EVM bit 2 and 4 gives this status,for 0 and 1
  100. * instance respectively.
  101. */
  102. MSP_GPIO(2, SDMMC), MSP_GPIO(1, SDMMC), /* mmc0 WP, nCD */
  103. MSP_GPIO(4, SDMMC), MSP_GPIO(3, SDMMC), /* mmc1 WP, nCD */
  104. };
  105. #define MSP_GPIO_REG(offset) (msp_gpios[(offset)] >> 3)
  106. #define MSP_GPIO_MASK(offset) BIT(msp_gpios[(offset)] & 0x07)
  107. static int msp_gpio_in(struct gpio_chip *chip, unsigned offset)
  108. {
  109. switch (MSP_GPIO_REG(offset)) {
  110. case DM355EVM_MSP_SWITCH1:
  111. case DM355EVM_MSP_SWITCH2:
  112. case DM355EVM_MSP_SDMMC:
  113. return 0;
  114. default:
  115. return -EINVAL;
  116. }
  117. }
  118. static u8 msp_led_cache;
  119. static int msp_gpio_get(struct gpio_chip *chip, unsigned offset)
  120. {
  121. int reg, status;
  122. reg = MSP_GPIO_REG(offset);
  123. status = dm355evm_msp_read(reg);
  124. if (status < 0)
  125. return status;
  126. if (reg == DM355EVM_MSP_LED)
  127. msp_led_cache = status;
  128. return status & MSP_GPIO_MASK(offset);
  129. }
  130. static int msp_gpio_out(struct gpio_chip *chip, unsigned offset, int value)
  131. {
  132. int mask, bits;
  133. /* NOTE: there are some other signals that could be
  134. * packaged as output GPIOs, but they aren't as useful
  135. * as the LEDs ... so for now we don't.
  136. */
  137. if (MSP_GPIO_REG(offset) != DM355EVM_MSP_LED)
  138. return -EINVAL;
  139. mask = MSP_GPIO_MASK(offset);
  140. bits = msp_led_cache;
  141. bits &= ~mask;
  142. if (value)
  143. bits |= mask;
  144. msp_led_cache = bits;
  145. return dm355evm_msp_write(bits, DM355EVM_MSP_LED);
  146. }
  147. static void msp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  148. {
  149. msp_gpio_out(chip, offset, value);
  150. }
  151. static struct gpio_chip dm355evm_msp_gpio = {
  152. .label = "dm355evm_msp",
  153. .owner = THIS_MODULE,
  154. .direction_input = msp_gpio_in,
  155. .get = msp_gpio_get,
  156. .direction_output = msp_gpio_out,
  157. .set = msp_gpio_set,
  158. .base = -EINVAL, /* dynamic assignment */
  159. .ngpio = ARRAY_SIZE(msp_gpios),
  160. .can_sleep = true,
  161. };
  162. /*----------------------------------------------------------------------*/
  163. static struct device *add_child(struct i2c_client *client, const char *name,
  164. void *pdata, unsigned pdata_len,
  165. bool can_wakeup, int irq)
  166. {
  167. struct platform_device *pdev;
  168. int status;
  169. pdev = platform_device_alloc(name, -1);
  170. if (!pdev) {
  171. dev_dbg(&client->dev, "can't alloc dev\n");
  172. status = -ENOMEM;
  173. goto err;
  174. }
  175. device_init_wakeup(&pdev->dev, can_wakeup);
  176. pdev->dev.parent = &client->dev;
  177. if (pdata) {
  178. status = platform_device_add_data(pdev, pdata, pdata_len);
  179. if (status < 0) {
  180. dev_dbg(&pdev->dev, "can't add platform_data\n");
  181. goto err;
  182. }
  183. }
  184. if (irq) {
  185. struct resource r = {
  186. .start = irq,
  187. .flags = IORESOURCE_IRQ,
  188. };
  189. status = platform_device_add_resources(pdev, &r, 1);
  190. if (status < 0) {
  191. dev_dbg(&pdev->dev, "can't add irq\n");
  192. goto err;
  193. }
  194. }
  195. status = platform_device_add(pdev);
  196. err:
  197. if (status < 0) {
  198. platform_device_put(pdev);
  199. dev_err(&client->dev, "can't add %s dev\n", name);
  200. return ERR_PTR(status);
  201. }
  202. return &pdev->dev;
  203. }
  204. static int add_children(struct i2c_client *client)
  205. {
  206. static const struct {
  207. int offset;
  208. char *label;
  209. } config_inputs[] = {
  210. /* 8 == right after the LEDs */
  211. { 8 + 0, "sw6_1", },
  212. { 8 + 1, "sw6_2", },
  213. { 8 + 2, "sw6_3", },
  214. { 8 + 3, "sw6_4", },
  215. { 8 + 4, "NTSC/nPAL", },
  216. };
  217. struct device *child;
  218. int status;
  219. int i;
  220. /* GPIO-ish stuff */
  221. dm355evm_msp_gpio.dev = &client->dev;
  222. status = gpiochip_add(&dm355evm_msp_gpio);
  223. if (status < 0)
  224. return status;
  225. /* LED output */
  226. if (msp_has_leds()) {
  227. #define GPIO_LED(l) .name = l, .active_low = true
  228. static struct gpio_led evm_leds[] = {
  229. { GPIO_LED("dm355evm::ds14"),
  230. .default_trigger = "heartbeat", },
  231. { GPIO_LED("dm355evm::ds15"),
  232. .default_trigger = "mmc0", },
  233. { GPIO_LED("dm355evm::ds16"),
  234. /* could also be a CE-ATA drive */
  235. .default_trigger = "mmc1", },
  236. { GPIO_LED("dm355evm::ds17"),
  237. .default_trigger = "nand-disk", },
  238. { GPIO_LED("dm355evm::ds18"), },
  239. { GPIO_LED("dm355evm::ds19"), },
  240. { GPIO_LED("dm355evm::ds20"), },
  241. { GPIO_LED("dm355evm::ds21"), },
  242. };
  243. #undef GPIO_LED
  244. struct gpio_led_platform_data evm_led_data = {
  245. .num_leds = ARRAY_SIZE(evm_leds),
  246. .leds = evm_leds,
  247. };
  248. for (i = 0; i < ARRAY_SIZE(evm_leds); i++)
  249. evm_leds[i].gpio = i + dm355evm_msp_gpio.base;
  250. /* NOTE: these are the only fully programmable LEDs
  251. * on the board, since GPIO-61/ds22 (and many signals
  252. * going to DC7) must be used for AEMIF address lines
  253. * unless the top 1 GB of NAND is unused...
  254. */
  255. child = add_child(client, "leds-gpio",
  256. &evm_led_data, sizeof(evm_led_data),
  257. false, 0);
  258. if (IS_ERR(child))
  259. return PTR_ERR(child);
  260. }
  261. /* configuration inputs */
  262. for (i = 0; i < ARRAY_SIZE(config_inputs); i++) {
  263. int gpio = dm355evm_msp_gpio.base + config_inputs[i].offset;
  264. gpio_request(gpio, config_inputs[i].label);
  265. gpio_direction_input(gpio);
  266. /* make it easy for userspace to see these */
  267. gpio_export(gpio, false);
  268. }
  269. /* MMC/SD inputs -- right after the last config input */
  270. if (client->dev.platform_data) {
  271. void (*mmcsd_setup)(unsigned) = client->dev.platform_data;
  272. mmcsd_setup(dm355evm_msp_gpio.base + 8 + 5);
  273. }
  274. /* RTC is a 32 bit counter, no alarm */
  275. if (msp_has_rtc()) {
  276. child = add_child(client, "rtc-dm355evm",
  277. NULL, 0, false, 0);
  278. if (IS_ERR(child))
  279. return PTR_ERR(child);
  280. }
  281. /* input from buttons and IR remote (uses the IRQ) */
  282. if (msp_has_keyboard()) {
  283. child = add_child(client, "dm355evm_keys",
  284. NULL, 0, true, client->irq);
  285. if (IS_ERR(child))
  286. return PTR_ERR(child);
  287. }
  288. return 0;
  289. }
  290. /*----------------------------------------------------------------------*/
  291. static void dm355evm_command(unsigned command)
  292. {
  293. int status;
  294. status = dm355evm_msp_write(command, DM355EVM_MSP_COMMAND);
  295. if (status < 0)
  296. dev_err(&msp430->dev, "command %d failure %d\n",
  297. command, status);
  298. }
  299. static void dm355evm_power_off(void)
  300. {
  301. dm355evm_command(MSP_COMMAND_POWEROFF);
  302. }
  303. static int dm355evm_msp_remove(struct i2c_client *client)
  304. {
  305. pm_power_off = NULL;
  306. msp430 = NULL;
  307. return 0;
  308. }
  309. static int
  310. dm355evm_msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
  311. {
  312. int status;
  313. const char *video = msp_has_tvp() ? "TVP5146" : "imager";
  314. if (msp430)
  315. return -EBUSY;
  316. msp430 = client;
  317. /* display revision status; doubles as sanity check */
  318. status = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
  319. if (status < 0)
  320. goto fail;
  321. dev_info(&client->dev, "firmware v.%02X, %s as video-in\n",
  322. status, video);
  323. /* mux video input: either tvp5146 or some external imager */
  324. status = dm355evm_msp_write(msp_has_tvp() ? 0 : MSP_VIDEO_IMAGER,
  325. DM355EVM_MSP_VIDEO_IN);
  326. if (status < 0)
  327. dev_warn(&client->dev, "error %d muxing %s as video-in\n",
  328. status, video);
  329. /* init LED cache, and turn off the LEDs */
  330. msp_led_cache = 0xff;
  331. dm355evm_msp_write(msp_led_cache, DM355EVM_MSP_LED);
  332. /* export capabilities we support */
  333. status = add_children(client);
  334. if (status < 0)
  335. goto fail;
  336. /* PM hookup */
  337. pm_power_off = dm355evm_power_off;
  338. return 0;
  339. fail:
  340. /* FIXME remove children ... */
  341. dm355evm_msp_remove(client);
  342. return status;
  343. }
  344. static const struct i2c_device_id dm355evm_msp_ids[] = {
  345. { "dm355evm_msp", 0 },
  346. { /* end of list */ },
  347. };
  348. MODULE_DEVICE_TABLE(i2c, dm355evm_msp_ids);
  349. static struct i2c_driver dm355evm_msp_driver = {
  350. .driver.name = "dm355evm_msp",
  351. .id_table = dm355evm_msp_ids,
  352. .probe = dm355evm_msp_probe,
  353. .remove = dm355evm_msp_remove,
  354. };
  355. static int __init dm355evm_msp_init(void)
  356. {
  357. return i2c_add_driver(&dm355evm_msp_driver);
  358. }
  359. subsys_initcall(dm355evm_msp_init);
  360. static void __exit dm355evm_msp_exit(void)
  361. {
  362. i2c_del_driver(&dm355evm_msp_driver);
  363. }
  364. module_exit(dm355evm_msp_exit);
  365. MODULE_DESCRIPTION("Interface to MSP430 firmware on DM355EVM");
  366. MODULE_LICENSE("GPL");