Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dm355evm_msp.c - driver for MSP430 firmware on DM355EVM board
0004  *
0005  * Copyright (C) 2008 David Brownell
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/mutex.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/clk.h>
0012 #include <linux/module.h>
0013 #include <linux/err.h>
0014 #include <linux/gpio.h>
0015 #include <linux/gpio/machine.h>
0016 #include <linux/leds.h>
0017 #include <linux/i2c.h>
0018 #include <linux/mfd/dm355evm_msp.h>
0019 
0020 
0021 /*
0022  * The DM355 is a DaVinci chip with video support but no C64+ DSP.  Its
0023  * EVM board has an MSP430 programmed with firmware for various board
0024  * support functions.  This driver exposes some of them directly, and
0025  * supports other drivers (e.g. RTC, input) for more complex access.
0026  *
0027  * Because this firmware is entirely board-specific, this file embeds
0028  * knowledge that would be passed as platform_data in a generic driver.
0029  *
0030  * This driver was tested with firmware revision A4.
0031  */
0032 
0033 #if IS_ENABLED(CONFIG_INPUT_DM355EVM)
0034 #define msp_has_keyboard()  true
0035 #else
0036 #define msp_has_keyboard()  false
0037 #endif
0038 
0039 #if IS_ENABLED(CONFIG_LEDS_GPIO)
0040 #define msp_has_leds()      true
0041 #else
0042 #define msp_has_leds()      false
0043 #endif
0044 
0045 #if IS_ENABLED(CONFIG_RTC_DRV_DM355EVM)
0046 #define msp_has_rtc()       true
0047 #else
0048 #define msp_has_rtc()       false
0049 #endif
0050 
0051 #if IS_ENABLED(CONFIG_VIDEO_TVP514X)
0052 #define msp_has_tvp()       true
0053 #else
0054 #define msp_has_tvp()       false
0055 #endif
0056 
0057 
0058 /*----------------------------------------------------------------------*/
0059 
0060 /* REVISIT for paranoia's sake, retry reads/writes on error */
0061 
0062 static struct i2c_client *msp430;
0063 
0064 /**
0065  * dm355evm_msp_write - Writes a register in dm355evm_msp
0066  * @value: the value to be written
0067  * @reg: register address
0068  *
0069  * Returns result of operation - 0 is success, else negative errno
0070  */
0071 int dm355evm_msp_write(u8 value, u8 reg)
0072 {
0073     return i2c_smbus_write_byte_data(msp430, reg, value);
0074 }
0075 EXPORT_SYMBOL(dm355evm_msp_write);
0076 
0077 /**
0078  * dm355evm_msp_read - Reads a register from dm355evm_msp
0079  * @reg: register address
0080  *
0081  * Returns result of operation - value, or negative errno
0082  */
0083 int dm355evm_msp_read(u8 reg)
0084 {
0085     return i2c_smbus_read_byte_data(msp430, reg);
0086 }
0087 EXPORT_SYMBOL(dm355evm_msp_read);
0088 
0089 /*----------------------------------------------------------------------*/
0090 
0091 /*
0092  * Many of the msp430 pins are just used as fixed-direction GPIOs.
0093  * We could export a few more of them this way, if we wanted.
0094  */
0095 #define MSP_GPIO(bit, reg)  ((DM355EVM_MSP_ ## reg) << 3 | (bit))
0096 
0097 static const u8 msp_gpios[] = {
0098     /* eight leds */
0099     MSP_GPIO(0, LED), MSP_GPIO(1, LED),
0100     MSP_GPIO(2, LED), MSP_GPIO(3, LED),
0101     MSP_GPIO(4, LED), MSP_GPIO(5, LED),
0102     MSP_GPIO(6, LED), MSP_GPIO(7, LED),
0103     /* SW6 and the NTSC/nPAL jumper */
0104     MSP_GPIO(0, SWITCH1), MSP_GPIO(1, SWITCH1),
0105     MSP_GPIO(2, SWITCH1), MSP_GPIO(3, SWITCH1),
0106     MSP_GPIO(4, SWITCH1),
0107     /* switches on MMC/SD sockets */
0108     /*
0109      * Note: EVMDM355_ECP_VA4.pdf suggests that Bit 2 and 4 should be
0110      * checked for card detection. However on the EVM bit 1 and 3 gives
0111      * this status, for 0 and 1 instance respectively. The pdf also
0112      * suggests that Bit 1 and 3 should be checked for write protection.
0113      * However on the EVM bit 2 and 4 gives this status,for 0 and 1
0114      * instance respectively.
0115      */
0116     MSP_GPIO(2, SDMMC), MSP_GPIO(1, SDMMC), /* mmc0 WP, nCD */
0117     MSP_GPIO(4, SDMMC), MSP_GPIO(3, SDMMC), /* mmc1 WP, nCD */
0118 };
0119 
0120 static struct gpio_led evm_leds[] = {
0121     { .name = "dm355evm::ds14",
0122       .default_trigger = "heartbeat", },
0123     { .name = "dm355evm::ds15",
0124       .default_trigger = "mmc0", },
0125     { .name = "dm355evm::ds16",
0126       /* could also be a CE-ATA drive */
0127       .default_trigger = "mmc1", },
0128     { .name = "dm355evm::ds17",
0129       .default_trigger = "nand-disk", },
0130     { .name = "dm355evm::ds18", },
0131     { .name = "dm355evm::ds19", },
0132     { .name = "dm355evm::ds20", },
0133     { .name = "dm355evm::ds21", },
0134 };
0135 
0136 static struct gpio_led_platform_data evm_led_data = {
0137     .num_leds   = ARRAY_SIZE(evm_leds),
0138     .leds       = evm_leds,
0139 };
0140 
0141 static struct gpiod_lookup_table evm_leds_gpio_table = {
0142     .dev_id = "leds-gpio",
0143     .table = {
0144         /*
0145          * These GPIOs are on the dm355evm_msp
0146          * GPIO chip at index 0..7
0147          */
0148         GPIO_LOOKUP_IDX("dm355evm_msp", 0, NULL,
0149                 0, GPIO_ACTIVE_LOW),
0150         GPIO_LOOKUP_IDX("dm355evm_msp", 1, NULL,
0151                 1, GPIO_ACTIVE_LOW),
0152         GPIO_LOOKUP_IDX("dm355evm_msp", 2, NULL,
0153                 2, GPIO_ACTIVE_LOW),
0154         GPIO_LOOKUP_IDX("dm355evm_msp", 3, NULL,
0155                 3, GPIO_ACTIVE_LOW),
0156         GPIO_LOOKUP_IDX("dm355evm_msp", 4, NULL,
0157                 4, GPIO_ACTIVE_LOW),
0158         GPIO_LOOKUP_IDX("dm355evm_msp", 5, NULL,
0159                 5, GPIO_ACTIVE_LOW),
0160         GPIO_LOOKUP_IDX("dm355evm_msp", 6, NULL,
0161                 6, GPIO_ACTIVE_LOW),
0162         GPIO_LOOKUP_IDX("dm355evm_msp", 7, NULL,
0163                 7, GPIO_ACTIVE_LOW),
0164         { },
0165     },
0166 };
0167 
0168 #define MSP_GPIO_REG(offset)    (msp_gpios[(offset)] >> 3)
0169 #define MSP_GPIO_MASK(offset)   BIT(msp_gpios[(offset)] & 0x07)
0170 
0171 static int msp_gpio_in(struct gpio_chip *chip, unsigned offset)
0172 {
0173     switch (MSP_GPIO_REG(offset)) {
0174     case DM355EVM_MSP_SWITCH1:
0175     case DM355EVM_MSP_SWITCH2:
0176     case DM355EVM_MSP_SDMMC:
0177         return 0;
0178     default:
0179         return -EINVAL;
0180     }
0181 }
0182 
0183 static u8 msp_led_cache;
0184 
0185 static int msp_gpio_get(struct gpio_chip *chip, unsigned offset)
0186 {
0187     int reg, status;
0188 
0189     reg = MSP_GPIO_REG(offset);
0190     status = dm355evm_msp_read(reg);
0191     if (status < 0)
0192         return status;
0193     if (reg == DM355EVM_MSP_LED)
0194         msp_led_cache = status;
0195     return !!(status & MSP_GPIO_MASK(offset));
0196 }
0197 
0198 static int msp_gpio_out(struct gpio_chip *chip, unsigned offset, int value)
0199 {
0200     int mask, bits;
0201 
0202     /* NOTE:  there are some other signals that could be
0203      * packaged as output GPIOs, but they aren't as useful
0204      * as the LEDs ... so for now we don't.
0205      */
0206     if (MSP_GPIO_REG(offset) != DM355EVM_MSP_LED)
0207         return -EINVAL;
0208 
0209     mask = MSP_GPIO_MASK(offset);
0210     bits = msp_led_cache;
0211 
0212     bits &= ~mask;
0213     if (value)
0214         bits |= mask;
0215     msp_led_cache = bits;
0216 
0217     return dm355evm_msp_write(bits, DM355EVM_MSP_LED);
0218 }
0219 
0220 static void msp_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
0221 {
0222     msp_gpio_out(chip, offset, value);
0223 }
0224 
0225 static struct gpio_chip dm355evm_msp_gpio = {
0226     .label          = "dm355evm_msp",
0227     .owner          = THIS_MODULE,
0228     .direction_input    = msp_gpio_in,
0229     .get            = msp_gpio_get,
0230     .direction_output   = msp_gpio_out,
0231     .set            = msp_gpio_set,
0232     .base           = -EINVAL,      /* dynamic assignment */
0233     .ngpio          = ARRAY_SIZE(msp_gpios),
0234     .can_sleep      = true,
0235 };
0236 
0237 /*----------------------------------------------------------------------*/
0238 
0239 static struct device *add_child(struct i2c_client *client, const char *name,
0240         void *pdata, unsigned pdata_len,
0241         bool can_wakeup, int irq)
0242 {
0243     struct platform_device  *pdev;
0244     int         status;
0245 
0246     pdev = platform_device_alloc(name, -1);
0247     if (!pdev)
0248         return ERR_PTR(-ENOMEM);
0249 
0250     device_init_wakeup(&pdev->dev, can_wakeup);
0251     pdev->dev.parent = &client->dev;
0252 
0253     if (pdata) {
0254         status = platform_device_add_data(pdev, pdata, pdata_len);
0255         if (status < 0) {
0256             dev_dbg(&pdev->dev, "can't add platform_data\n");
0257             goto put_device;
0258         }
0259     }
0260 
0261     if (irq) {
0262         struct resource r = {
0263             .start = irq,
0264             .flags = IORESOURCE_IRQ,
0265         };
0266 
0267         status = platform_device_add_resources(pdev, &r, 1);
0268         if (status < 0) {
0269             dev_dbg(&pdev->dev, "can't add irq\n");
0270             goto put_device;
0271         }
0272     }
0273 
0274     status = platform_device_add(pdev);
0275     if (status)
0276         goto put_device;
0277 
0278     return &pdev->dev;
0279 
0280 put_device:
0281     platform_device_put(pdev);
0282     dev_err(&client->dev, "failed to add device %s\n", name);
0283     return ERR_PTR(status);
0284 }
0285 
0286 static int add_children(struct i2c_client *client)
0287 {
0288     static const struct {
0289         int offset;
0290         char *label;
0291     } config_inputs[] = {
0292         /* 8 == right after the LEDs */
0293         { 8 + 0, "sw6_1", },
0294         { 8 + 1, "sw6_2", },
0295         { 8 + 2, "sw6_3", },
0296         { 8 + 3, "sw6_4", },
0297         { 8 + 4, "NTSC/nPAL", },
0298     };
0299 
0300     struct device   *child;
0301     int     status;
0302     int     i;
0303 
0304     /* GPIO-ish stuff */
0305     dm355evm_msp_gpio.parent = &client->dev;
0306     status = gpiochip_add_data(&dm355evm_msp_gpio, NULL);
0307     if (status < 0)
0308         return status;
0309 
0310     /* LED output */
0311     if (msp_has_leds()) {
0312         gpiod_add_lookup_table(&evm_leds_gpio_table);
0313         /* NOTE:  these are the only fully programmable LEDs
0314          * on the board, since GPIO-61/ds22 (and many signals
0315          * going to DC7) must be used for AEMIF address lines
0316          * unless the top 1 GB of NAND is unused...
0317          */
0318         child = add_child(client, "leds-gpio",
0319                 &evm_led_data, sizeof(evm_led_data),
0320                 false, 0);
0321         if (IS_ERR(child))
0322             return PTR_ERR(child);
0323     }
0324 
0325     /* configuration inputs */
0326     for (i = 0; i < ARRAY_SIZE(config_inputs); i++) {
0327         int gpio = dm355evm_msp_gpio.base + config_inputs[i].offset;
0328 
0329         gpio_request_one(gpio, GPIOF_IN, config_inputs[i].label);
0330 
0331         /* make it easy for userspace to see these */
0332         gpio_export(gpio, false);
0333     }
0334 
0335     /* MMC/SD inputs -- right after the last config input */
0336     if (dev_get_platdata(&client->dev)) {
0337         void (*mmcsd_setup)(unsigned) = dev_get_platdata(&client->dev);
0338 
0339         mmcsd_setup(dm355evm_msp_gpio.base + 8 + 5);
0340     }
0341 
0342     /* RTC is a 32 bit counter, no alarm */
0343     if (msp_has_rtc()) {
0344         child = add_child(client, "rtc-dm355evm",
0345                 NULL, 0, false, 0);
0346         if (IS_ERR(child))
0347             return PTR_ERR(child);
0348     }
0349 
0350     /* input from buttons and IR remote (uses the IRQ) */
0351     if (msp_has_keyboard()) {
0352         child = add_child(client, "dm355evm_keys",
0353                 NULL, 0, true, client->irq);
0354         if (IS_ERR(child))
0355             return PTR_ERR(child);
0356     }
0357 
0358     return 0;
0359 }
0360 
0361 /*----------------------------------------------------------------------*/
0362 
0363 static void dm355evm_command(unsigned command)
0364 {
0365     int status;
0366 
0367     status = dm355evm_msp_write(command, DM355EVM_MSP_COMMAND);
0368     if (status < 0)
0369         dev_err(&msp430->dev, "command %d failure %d\n",
0370                 command, status);
0371 }
0372 
0373 static void dm355evm_power_off(void)
0374 {
0375     dm355evm_command(MSP_COMMAND_POWEROFF);
0376 }
0377 
0378 static int dm355evm_msp_remove(struct i2c_client *client)
0379 {
0380     pm_power_off = NULL;
0381     msp430 = NULL;
0382     return 0;
0383 }
0384 
0385 static int
0386 dm355evm_msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
0387 {
0388     int     status;
0389     const char  *video = msp_has_tvp() ? "TVP5146" : "imager";
0390 
0391     if (msp430)
0392         return -EBUSY;
0393     msp430 = client;
0394 
0395     /* display revision status; doubles as sanity check */
0396     status = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
0397     if (status < 0)
0398         goto fail;
0399     dev_info(&client->dev, "firmware v.%02X, %s as video-in\n",
0400             status, video);
0401 
0402     /* mux video input:  either tvp5146 or some external imager */
0403     status = dm355evm_msp_write(msp_has_tvp() ? 0 : MSP_VIDEO_IMAGER,
0404             DM355EVM_MSP_VIDEO_IN);
0405     if (status < 0)
0406         dev_warn(&client->dev, "error %d muxing %s as video-in\n",
0407             status, video);
0408 
0409     /* init LED cache, and turn off the LEDs */
0410     msp_led_cache = 0xff;
0411     dm355evm_msp_write(msp_led_cache, DM355EVM_MSP_LED);
0412 
0413     /* export capabilities we support */
0414     status = add_children(client);
0415     if (status < 0)
0416         goto fail;
0417 
0418     /* PM hookup */
0419     pm_power_off = dm355evm_power_off;
0420 
0421     return 0;
0422 
0423 fail:
0424     /* FIXME remove children ... */
0425     dm355evm_msp_remove(client);
0426     return status;
0427 }
0428 
0429 static const struct i2c_device_id dm355evm_msp_ids[] = {
0430     { "dm355evm_msp", 0 },
0431     { /* end of list */ },
0432 };
0433 MODULE_DEVICE_TABLE(i2c, dm355evm_msp_ids);
0434 
0435 static struct i2c_driver dm355evm_msp_driver = {
0436     .driver.name    = "dm355evm_msp",
0437     .id_table   = dm355evm_msp_ids,
0438     .probe      = dm355evm_msp_probe,
0439     .remove     = dm355evm_msp_remove,
0440 };
0441 
0442 static int __init dm355evm_msp_init(void)
0443 {
0444     return i2c_add_driver(&dm355evm_msp_driver);
0445 }
0446 subsys_initcall(dm355evm_msp_init);
0447 
0448 static void __exit dm355evm_msp_exit(void)
0449 {
0450     i2c_del_driver(&dm355evm_msp_driver);
0451 }
0452 module_exit(dm355evm_msp_exit);
0453 
0454 MODULE_DESCRIPTION("Interface to MSP430 firmware on DM355EVM");
0455 MODULE_LICENSE("GPL");