2 * dm355evm_msp.c - driver for MSP430 firmware on DM355EVM board
4 * Copyright (C) 2008 David Brownell
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.
12 #include <linux/init.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
18 #include <linux/gpio.h>
19 #include <linux/leds.h>
20 #include <linux/i2c.h>
21 #include <linux/mfd/dm355evm_msp.h>
25 * The DM355 is a DaVinci chip with video support but no C64+ DSP. Its
26 * EVM board has an MSP430 programmed with firmware for various board
27 * support functions. This driver exposes some of them directly, and
28 * supports other drivers (e.g. RTC, input) for more complex access.
30 * Because this firmware is entirely board-specific, this file embeds
31 * knowledge that would be passed as platform_data in a generic driver.
33 * This driver was tested with firmware revision A4.
36 #if IS_ENABLED(CONFIG_INPUT_DM355EVM)
37 #define msp_has_keyboard() true
39 #define msp_has_keyboard() false
42 #if IS_ENABLED(CONFIG_LEDS_GPIO)
43 #define msp_has_leds() true
45 #define msp_has_leds() false
48 #if IS_ENABLED(CONFIG_RTC_DRV_DM355EVM)
49 #define msp_has_rtc() true
51 #define msp_has_rtc() false
54 #if IS_ENABLED(CONFIG_VIDEO_TVP514X)
55 #define msp_has_tvp() true
57 #define msp_has_tvp() false
61 /*----------------------------------------------------------------------*/
63 /* REVISIT for paranoia's sake, retry reads/writes on error */
65 static struct i2c_client *msp430;
68 * dm355evm_msp_write - Writes a register in dm355evm_msp
69 * @value: the value to be written
70 * @reg: register address
72 * Returns result of operation - 0 is success, else negative errno
74 int dm355evm_msp_write(u8 value, u8 reg)
76 return i2c_smbus_write_byte_data(msp430, reg, value);
78 EXPORT_SYMBOL(dm355evm_msp_write);
81 * dm355evm_msp_read - Reads a register from dm355evm_msp
82 * @reg: register address
84 * Returns result of operation - value, or negative errno
86 int dm355evm_msp_read(u8 reg)
88 return i2c_smbus_read_byte_data(msp430, reg);
90 EXPORT_SYMBOL(dm355evm_msp_read);
92 /*----------------------------------------------------------------------*/
95 * Many of the msp430 pins are just used as fixed-direction GPIOs.
96 * We could export a few more of them this way, if we wanted.
98 #define MSP_GPIO(bit, reg) ((DM355EVM_MSP_ ## reg) << 3 | (bit))
100 static const u8 msp_gpios[] = {
102 MSP_GPIO(0, LED), MSP_GPIO(1, LED),
103 MSP_GPIO(2, LED), MSP_GPIO(3, LED),
104 MSP_GPIO(4, LED), MSP_GPIO(5, LED),
105 MSP_GPIO(6, LED), MSP_GPIO(7, LED),
106 /* SW6 and the NTSC/nPAL jumper */
107 MSP_GPIO(0, SWITCH1), MSP_GPIO(1, SWITCH1),
108 MSP_GPIO(2, SWITCH1), MSP_GPIO(3, SWITCH1),
109 MSP_GPIO(4, SWITCH1),
110 /* switches on MMC/SD sockets */
112 * Note: EVMDM355_ECP_VA4.pdf suggests that Bit 2 and 4 should be
113 * checked for card detection. However on the EVM bit 1 and 3 gives
114 * this status, for 0 and 1 instance respectively. The pdf also
115 * suggests that Bit 1 and 3 should be checked for write protection.
116 * However on the EVM bit 2 and 4 gives this status,for 0 and 1
117 * instance respectively.
119 MSP_GPIO(2, SDMMC), MSP_GPIO(1, SDMMC), /* mmc0 WP, nCD */
120 MSP_GPIO(4, SDMMC), MSP_GPIO(3, SDMMC), /* mmc1 WP, nCD */
123 #define MSP_GPIO_REG(offset) (msp_gpios[(offset)] >> 3)
124 #define MSP_GPIO_MASK(offset) BIT(msp_gpios[(offset)] & 0x07)
126 static int msp_gpio_in(struct gpio_chip *chip, unsigned offset)
128 switch (MSP_GPIO_REG(offset)) {
129 case DM355EVM_MSP_SWITCH1:
130 case DM355EVM_MSP_SWITCH2:
131 case DM355EVM_MSP_SDMMC:
138 static u8 msp_led_cache;
140 static int msp_gpio_get(struct gpio_chip *chip, unsigned offset)
144 reg = MSP_GPIO_REG(offset);
145 status = dm355evm_msp_read(reg);
148 if (reg == DM355EVM_MSP_LED)
149 msp_led_cache = status;
150 return !!(status & MSP_GPIO_MASK(offset));
153 static int msp_gpio_out(struct gpio_chip *chip, unsigned offset, int value)
157 /* NOTE: there are some other signals that could be
158 * packaged as output GPIOs, but they aren't as useful
159 * as the LEDs ... so for now we don't.
161 if (MSP_GPIO_REG(offset) != DM355EVM_MSP_LED)
164 mask = MSP_GPIO_MASK(offset);
165 bits = msp_led_cache;
170 msp_led_cache = bits;
172 return dm355evm_msp_write(bits, DM355EVM_MSP_LED);
175 static void msp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
177 msp_gpio_out(chip, offset, value);
180 static struct gpio_chip dm355evm_msp_gpio = {
181 .label = "dm355evm_msp",
182 .owner = THIS_MODULE,
183 .direction_input = msp_gpio_in,
185 .direction_output = msp_gpio_out,
187 .base = -EINVAL, /* dynamic assignment */
188 .ngpio = ARRAY_SIZE(msp_gpios),
192 /*----------------------------------------------------------------------*/
194 static struct device *add_child(struct i2c_client *client, const char *name,
195 void *pdata, unsigned pdata_len,
196 bool can_wakeup, int irq)
198 struct platform_device *pdev;
201 pdev = platform_device_alloc(name, -1);
203 return ERR_PTR(-ENOMEM);
205 device_init_wakeup(&pdev->dev, can_wakeup);
206 pdev->dev.parent = &client->dev;
209 status = platform_device_add_data(pdev, pdata, pdata_len);
211 dev_dbg(&pdev->dev, "can't add platform_data\n");
217 struct resource r = {
219 .flags = IORESOURCE_IRQ,
222 status = platform_device_add_resources(pdev, &r, 1);
224 dev_dbg(&pdev->dev, "can't add irq\n");
229 status = platform_device_add(pdev);
236 platform_device_put(pdev);
237 dev_err(&client->dev, "failed to add device %s\n", name);
238 return ERR_PTR(status);
241 static int add_children(struct i2c_client *client)
243 static const struct {
246 } config_inputs[] = {
247 /* 8 == right after the LEDs */
252 { 8 + 4, "NTSC/nPAL", },
255 struct device *child;
260 dm355evm_msp_gpio.parent = &client->dev;
261 status = gpiochip_add_data(&dm355evm_msp_gpio, NULL);
266 if (msp_has_leds()) {
267 #define GPIO_LED(l) .name = l, .active_low = true
268 static struct gpio_led evm_leds[] = {
269 { GPIO_LED("dm355evm::ds14"),
270 .default_trigger = "heartbeat", },
271 { GPIO_LED("dm355evm::ds15"),
272 .default_trigger = "mmc0", },
273 { GPIO_LED("dm355evm::ds16"),
274 /* could also be a CE-ATA drive */
275 .default_trigger = "mmc1", },
276 { GPIO_LED("dm355evm::ds17"),
277 .default_trigger = "nand-disk", },
278 { GPIO_LED("dm355evm::ds18"), },
279 { GPIO_LED("dm355evm::ds19"), },
280 { GPIO_LED("dm355evm::ds20"), },
281 { GPIO_LED("dm355evm::ds21"), },
285 struct gpio_led_platform_data evm_led_data = {
286 .num_leds = ARRAY_SIZE(evm_leds),
290 for (i = 0; i < ARRAY_SIZE(evm_leds); i++)
291 evm_leds[i].gpio = i + dm355evm_msp_gpio.base;
293 /* NOTE: these are the only fully programmable LEDs
294 * on the board, since GPIO-61/ds22 (and many signals
295 * going to DC7) must be used for AEMIF address lines
296 * unless the top 1 GB of NAND is unused...
298 child = add_child(client, "leds-gpio",
299 &evm_led_data, sizeof(evm_led_data),
302 return PTR_ERR(child);
305 /* configuration inputs */
306 for (i = 0; i < ARRAY_SIZE(config_inputs); i++) {
307 int gpio = dm355evm_msp_gpio.base + config_inputs[i].offset;
309 gpio_request_one(gpio, GPIOF_IN, config_inputs[i].label);
311 /* make it easy for userspace to see these */
312 gpio_export(gpio, false);
315 /* MMC/SD inputs -- right after the last config input */
316 if (dev_get_platdata(&client->dev)) {
317 void (*mmcsd_setup)(unsigned) = dev_get_platdata(&client->dev);
319 mmcsd_setup(dm355evm_msp_gpio.base + 8 + 5);
322 /* RTC is a 32 bit counter, no alarm */
324 child = add_child(client, "rtc-dm355evm",
327 return PTR_ERR(child);
330 /* input from buttons and IR remote (uses the IRQ) */
331 if (msp_has_keyboard()) {
332 child = add_child(client, "dm355evm_keys",
333 NULL, 0, true, client->irq);
335 return PTR_ERR(child);
341 /*----------------------------------------------------------------------*/
343 static void dm355evm_command(unsigned command)
347 status = dm355evm_msp_write(command, DM355EVM_MSP_COMMAND);
349 dev_err(&msp430->dev, "command %d failure %d\n",
353 static void dm355evm_power_off(void)
355 dm355evm_command(MSP_COMMAND_POWEROFF);
358 static int dm355evm_msp_remove(struct i2c_client *client)
366 dm355evm_msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
369 const char *video = msp_has_tvp() ? "TVP5146" : "imager";
375 /* display revision status; doubles as sanity check */
376 status = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
379 dev_info(&client->dev, "firmware v.%02X, %s as video-in\n",
382 /* mux video input: either tvp5146 or some external imager */
383 status = dm355evm_msp_write(msp_has_tvp() ? 0 : MSP_VIDEO_IMAGER,
384 DM355EVM_MSP_VIDEO_IN);
386 dev_warn(&client->dev, "error %d muxing %s as video-in\n",
389 /* init LED cache, and turn off the LEDs */
390 msp_led_cache = 0xff;
391 dm355evm_msp_write(msp_led_cache, DM355EVM_MSP_LED);
393 /* export capabilities we support */
394 status = add_children(client);
399 pm_power_off = dm355evm_power_off;
404 /* FIXME remove children ... */
405 dm355evm_msp_remove(client);
409 static const struct i2c_device_id dm355evm_msp_ids[] = {
410 { "dm355evm_msp", 0 },
411 { /* end of list */ },
413 MODULE_DEVICE_TABLE(i2c, dm355evm_msp_ids);
415 static struct i2c_driver dm355evm_msp_driver = {
416 .driver.name = "dm355evm_msp",
417 .id_table = dm355evm_msp_ids,
418 .probe = dm355evm_msp_probe,
419 .remove = dm355evm_msp_remove,
422 static int __init dm355evm_msp_init(void)
424 return i2c_add_driver(&dm355evm_msp_driver);
426 subsys_initcall(dm355evm_msp_init);
428 static void __exit dm355evm_msp_exit(void)
430 i2c_del_driver(&dm355evm_msp_driver);
432 module_exit(dm355evm_msp_exit);
434 MODULE_DESCRIPTION("Interface to MSP430 firmware on DM355EVM");
435 MODULE_LICENSE("GPL");