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