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