dm355evm_msp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. MSP_GPIO(1, SDMMC), MSP_GPIO(2, SDMMC), /* mmc0 WP, nCD */
  95. MSP_GPIO(3, SDMMC), MSP_GPIO(4, SDMMC), /* mmc1 WP, nCD */
  96. };
  97. #define MSP_GPIO_REG(offset) (msp_gpios[(offset)] >> 3)
  98. #define MSP_GPIO_MASK(offset) BIT(msp_gpios[(offset)] & 0x07)
  99. static int msp_gpio_in(struct gpio_chip *chip, unsigned offset)
  100. {
  101. switch (MSP_GPIO_REG(offset)) {
  102. case DM355EVM_MSP_SWITCH1:
  103. case DM355EVM_MSP_SWITCH2:
  104. case DM355EVM_MSP_SDMMC:
  105. return 0;
  106. default:
  107. return -EINVAL;
  108. }
  109. }
  110. static u8 msp_led_cache;
  111. static int msp_gpio_get(struct gpio_chip *chip, unsigned offset)
  112. {
  113. int reg, status;
  114. reg = MSP_GPIO_REG(offset);
  115. status = dm355evm_msp_read(reg);
  116. if (status < 0)
  117. return status;
  118. if (reg == DM355EVM_MSP_LED)
  119. msp_led_cache = status;
  120. return status & MSP_GPIO_MASK(offset);
  121. }
  122. static int msp_gpio_out(struct gpio_chip *chip, unsigned offset, int value)
  123. {
  124. int mask, bits;
  125. /* NOTE: there are some other signals that could be
  126. * packaged as output GPIOs, but they aren't as useful
  127. * as the LEDs ... so for now we don't.
  128. */
  129. if (MSP_GPIO_REG(offset) != DM355EVM_MSP_LED)
  130. return -EINVAL;
  131. mask = MSP_GPIO_MASK(offset);
  132. bits = msp_led_cache;
  133. bits &= ~mask;
  134. if (value)
  135. bits |= mask;
  136. msp_led_cache = bits;
  137. return dm355evm_msp_write(bits, DM355EVM_MSP_LED);
  138. }
  139. static void msp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  140. {
  141. msp_gpio_out(chip, offset, value);
  142. }
  143. static struct gpio_chip dm355evm_msp_gpio = {
  144. .label = "dm355evm_msp",
  145. .owner = THIS_MODULE,
  146. .direction_input = msp_gpio_in,
  147. .get = msp_gpio_get,
  148. .direction_output = msp_gpio_out,
  149. .set = msp_gpio_set,
  150. .base = -EINVAL, /* dynamic assignment */
  151. .ngpio = ARRAY_SIZE(msp_gpios),
  152. .can_sleep = true,
  153. };
  154. /*----------------------------------------------------------------------*/
  155. static struct device *add_child(struct i2c_client *client, const char *name,
  156. void *pdata, unsigned pdata_len,
  157. bool can_wakeup, int irq)
  158. {
  159. struct platform_device *pdev;
  160. int status;
  161. pdev = platform_device_alloc(name, -1);
  162. if (!pdev) {
  163. dev_dbg(&client->dev, "can't alloc dev\n");
  164. status = -ENOMEM;
  165. goto err;
  166. }
  167. device_init_wakeup(&pdev->dev, can_wakeup);
  168. pdev->dev.parent = &client->dev;
  169. if (pdata) {
  170. status = platform_device_add_data(pdev, pdata, pdata_len);
  171. if (status < 0) {
  172. dev_dbg(&pdev->dev, "can't add platform_data\n");
  173. goto err;
  174. }
  175. }
  176. if (irq) {
  177. struct resource r = {
  178. .start = irq,
  179. .flags = IORESOURCE_IRQ,
  180. };
  181. status = platform_device_add_resources(pdev, &r, 1);
  182. if (status < 0) {
  183. dev_dbg(&pdev->dev, "can't add irq\n");
  184. goto err;
  185. }
  186. }
  187. status = platform_device_add(pdev);
  188. err:
  189. if (status < 0) {
  190. platform_device_put(pdev);
  191. dev_err(&client->dev, "can't add %s dev\n", name);
  192. return ERR_PTR(status);
  193. }
  194. return &pdev->dev;
  195. }
  196. static int add_children(struct i2c_client *client)
  197. {
  198. static const struct {
  199. int offset;
  200. char *label;
  201. } config_inputs[] = {
  202. /* 8 == right after the LEDs */
  203. { 8 + 0, "sw6_1", },
  204. { 8 + 1, "sw6_2", },
  205. { 8 + 2, "sw6_3", },
  206. { 8 + 3, "sw6_4", },
  207. { 8 + 4, "NTSC/nPAL", },
  208. };
  209. struct device *child;
  210. int status;
  211. int i;
  212. /* GPIO-ish stuff */
  213. dm355evm_msp_gpio.dev = &client->dev;
  214. status = gpiochip_add(&dm355evm_msp_gpio);
  215. if (status < 0)
  216. return status;
  217. /* LED output */
  218. if (msp_has_leds()) {
  219. #define GPIO_LED(l) .name = l, .active_low = true
  220. static struct gpio_led evm_leds[] = {
  221. { GPIO_LED("dm355evm::ds14"),
  222. .default_trigger = "heartbeat", },
  223. { GPIO_LED("dm355evm::ds15"),
  224. .default_trigger = "mmc0", },
  225. { GPIO_LED("dm355evm::ds16"),
  226. /* could also be a CE-ATA drive */
  227. .default_trigger = "mmc1", },
  228. { GPIO_LED("dm355evm::ds17"),
  229. .default_trigger = "nand-disk", },
  230. { GPIO_LED("dm355evm::ds18"), },
  231. { GPIO_LED("dm355evm::ds19"), },
  232. { GPIO_LED("dm355evm::ds20"), },
  233. { GPIO_LED("dm355evm::ds21"), },
  234. };
  235. #undef GPIO_LED
  236. struct gpio_led_platform_data evm_led_data = {
  237. .num_leds = ARRAY_SIZE(evm_leds),
  238. .leds = evm_leds,
  239. };
  240. for (i = 0; i < ARRAY_SIZE(evm_leds); i++)
  241. evm_leds[i].gpio = i + dm355evm_msp_gpio.base;
  242. /* NOTE: these are the only fully programmable LEDs
  243. * on the board, since GPIO-61/ds22 (and many signals
  244. * going to DC7) must be used for AEMIF address lines
  245. * unless the top 1 GB of NAND is unused...
  246. */
  247. child = add_child(client, "leds-gpio",
  248. &evm_led_data, sizeof(evm_led_data),
  249. false, 0);
  250. if (IS_ERR(child))
  251. return PTR_ERR(child);
  252. }
  253. /* configuration inputs */
  254. for (i = 0; i < ARRAY_SIZE(config_inputs); i++) {
  255. int gpio = dm355evm_msp_gpio.base + config_inputs[i].offset;
  256. gpio_request(gpio, config_inputs[i].label);
  257. gpio_direction_input(gpio);
  258. /* make it easy for userspace to see these */
  259. gpio_export(gpio, false);
  260. }
  261. /* MMC/SD inputs -- right after the last config input */
  262. if (client->dev.platform_data) {
  263. void (*mmcsd_setup)(unsigned) = client->dev.platform_data;
  264. mmcsd_setup(dm355evm_msp_gpio.base + 8 + 5);
  265. }
  266. /* RTC is a 32 bit counter, no alarm */
  267. if (msp_has_rtc()) {
  268. child = add_child(client, "rtc-dm355evm",
  269. NULL, 0, false, 0);
  270. if (IS_ERR(child))
  271. return PTR_ERR(child);
  272. }
  273. /* input from buttons and IR remote (uses the IRQ) */
  274. if (msp_has_keyboard()) {
  275. child = add_child(client, "dm355evm_keys",
  276. NULL, 0, true, client->irq);
  277. if (IS_ERR(child))
  278. return PTR_ERR(child);
  279. }
  280. return 0;
  281. }
  282. /*----------------------------------------------------------------------*/
  283. static void dm355evm_command(unsigned command)
  284. {
  285. int status;
  286. status = dm355evm_msp_write(command, DM355EVM_MSP_COMMAND);
  287. if (status < 0)
  288. dev_err(&msp430->dev, "command %d failure %d\n",
  289. command, status);
  290. }
  291. static void dm355evm_power_off(void)
  292. {
  293. dm355evm_command(MSP_COMMAND_POWEROFF);
  294. }
  295. static int dm355evm_msp_remove(struct i2c_client *client)
  296. {
  297. pm_power_off = NULL;
  298. msp430 = NULL;
  299. return 0;
  300. }
  301. static int
  302. dm355evm_msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
  303. {
  304. int status;
  305. const char *video = msp_has_tvp() ? "TVP5146" : "imager";
  306. if (msp430)
  307. return -EBUSY;
  308. msp430 = client;
  309. /* display revision status; doubles as sanity check */
  310. status = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
  311. if (status < 0)
  312. goto fail;
  313. dev_info(&client->dev, "firmware v.%02X, %s as video-in\n",
  314. status, video);
  315. /* mux video input: either tvp5146 or some external imager */
  316. status = dm355evm_msp_write(msp_has_tvp() ? 0 : MSP_VIDEO_IMAGER,
  317. DM355EVM_MSP_VIDEO_IN);
  318. if (status < 0)
  319. dev_warn(&client->dev, "error %d muxing %s as video-in\n",
  320. status, video);
  321. /* init LED cache, and turn off the LEDs */
  322. msp_led_cache = 0xff;
  323. dm355evm_msp_write(msp_led_cache, DM355EVM_MSP_LED);
  324. /* export capabilities we support */
  325. status = add_children(client);
  326. if (status < 0)
  327. goto fail;
  328. /* PM hookup */
  329. pm_power_off = dm355evm_power_off;
  330. return 0;
  331. fail:
  332. /* FIXME remove children ... */
  333. dm355evm_msp_remove(client);
  334. return status;
  335. }
  336. static const struct i2c_device_id dm355evm_msp_ids[] = {
  337. { "dm355evm_msp", 0 },
  338. { /* end of list */ },
  339. };
  340. MODULE_DEVICE_TABLE(i2c, dm355evm_msp_ids);
  341. static struct i2c_driver dm355evm_msp_driver = {
  342. .driver.name = "dm355evm_msp",
  343. .id_table = dm355evm_msp_ids,
  344. .probe = dm355evm_msp_probe,
  345. .remove = dm355evm_msp_remove,
  346. };
  347. static int __init dm355evm_msp_init(void)
  348. {
  349. return i2c_add_driver(&dm355evm_msp_driver);
  350. }
  351. subsys_initcall(dm355evm_msp_init);
  352. static void __exit dm355evm_msp_exit(void)
  353. {
  354. i2c_del_driver(&dm355evm_msp_driver);
  355. }
  356. module_exit(dm355evm_msp_exit);
  357. MODULE_DESCRIPTION("Interface to MSP430 firmware on DM355EVM");
  358. MODULE_LICENSE("GPL");