dm355evm_msp.c 10 KB

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