dm355evm_msp.c 11 KB

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