dm355evm_msp.c 10 KB

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