Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright 2009-2010 Pengutronix
0004  * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
0005  *
0006  * loosely based on an earlier driver that has
0007  * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
0008  */
0009 
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/mfd/core.h>
0015 #include <linux/mfd/mc13xxx.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <linux/of_gpio.h>
0019 #include <linux/err.h>
0020 #include <linux/spi/spi.h>
0021 
0022 #include "mc13xxx.h"
0023 
0024 static const struct spi_device_id mc13xxx_device_id[] = {
0025     {
0026         .name = "mc13783",
0027         .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783,
0028     }, {
0029         .name = "mc13892",
0030         .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
0031     }, {
0032         .name = "mc34708",
0033         .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
0034     }, {
0035         /* sentinel */
0036     }
0037 };
0038 MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
0039 
0040 static const struct of_device_id mc13xxx_dt_ids[] = {
0041     { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, },
0042     { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, },
0043     { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, },
0044     { /* sentinel */ }
0045 };
0046 MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
0047 
0048 static const struct regmap_config mc13xxx_regmap_spi_config = {
0049     .reg_bits = 7,
0050     .pad_bits = 1,
0051     .val_bits = 24,
0052     .write_flag_mask = 0x80,
0053 
0054     .max_register = MC13XXX_NUMREGS,
0055 
0056     .cache_type = REGCACHE_NONE,
0057     .use_single_read = true,
0058     .use_single_write = true,
0059 };
0060 
0061 static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size,
0062                 void *val, size_t val_size)
0063 {
0064     unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0};
0065     unsigned char r[4];
0066     unsigned char *p = val;
0067     struct device *dev = context;
0068     struct spi_device *spi = to_spi_device(dev);
0069     struct spi_transfer t = {
0070         .tx_buf = w,
0071         .rx_buf = r,
0072         .len = 4,
0073     };
0074 
0075     struct spi_message m;
0076     int ret;
0077 
0078     if (val_size != 3 || reg_size != 1)
0079         return -ENOTSUPP;
0080 
0081     spi_message_init(&m);
0082     spi_message_add_tail(&t, &m);
0083     ret = spi_sync(spi, &m);
0084 
0085     memcpy(p, &r[1], 3);
0086 
0087     return ret;
0088 }
0089 
0090 static int mc13xxx_spi_write(void *context, const void *data, size_t count)
0091 {
0092     struct device *dev = context;
0093     struct spi_device *spi = to_spi_device(dev);
0094     const char *reg = data;
0095 
0096     if (count != 4)
0097         return -ENOTSUPP;
0098 
0099     /* include errata fix for spi audio problems */
0100     if (*reg == MC13783_AUDIO_CODEC || *reg == MC13783_AUDIO_DAC)
0101         spi_write(spi, data, count);
0102 
0103     return spi_write(spi, data, count);
0104 }
0105 
0106 /*
0107  * We cannot use regmap-spi generic bus implementation here.
0108  * The MC13783 chip will get corrupted if CS signal is deasserted
0109  * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
0110  * has the following errata (DSPhl22960):
0111  * "The CSPI negates SS when the FIFO becomes empty with
0112  * SSCTL= 0. Software cannot guarantee that the FIFO will not
0113  * drain because of higher priority interrupts and the
0114  * non-realtime characteristics of the operating system. As a
0115  * result, the SS will negate before all of the data has been
0116  * transferred to/from the peripheral."
0117  * We workaround this by accessing the SPI controller with a
0118  * single transfert.
0119  */
0120 
0121 static struct regmap_bus regmap_mc13xxx_bus = {
0122     .write = mc13xxx_spi_write,
0123     .read = mc13xxx_spi_read,
0124 };
0125 
0126 static int mc13xxx_spi_probe(struct spi_device *spi)
0127 {
0128     struct mc13xxx *mc13xxx;
0129     int ret;
0130 
0131     mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL);
0132     if (!mc13xxx)
0133         return -ENOMEM;
0134 
0135     dev_set_drvdata(&spi->dev, mc13xxx);
0136 
0137     spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
0138 
0139     mc13xxx->irq = spi->irq;
0140 
0141     spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
0142     ret = spi_setup(spi);
0143     if (ret)
0144         return ret;
0145 
0146     mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
0147                        &spi->dev,
0148                        &mc13xxx_regmap_spi_config);
0149     if (IS_ERR(mc13xxx->regmap)) {
0150         ret = PTR_ERR(mc13xxx->regmap);
0151         dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret);
0152         return ret;
0153     }
0154 
0155     if (spi->dev.of_node) {
0156         const struct of_device_id *of_id =
0157             of_match_device(mc13xxx_dt_ids, &spi->dev);
0158 
0159         mc13xxx->variant = of_id->data;
0160     } else {
0161         const struct spi_device_id *id_entry = spi_get_device_id(spi);
0162 
0163         mc13xxx->variant = (void *)id_entry->driver_data;
0164     }
0165 
0166     return mc13xxx_common_init(&spi->dev);
0167 }
0168 
0169 static void mc13xxx_spi_remove(struct spi_device *spi)
0170 {
0171     mc13xxx_common_exit(&spi->dev);
0172 }
0173 
0174 static struct spi_driver mc13xxx_spi_driver = {
0175     .id_table = mc13xxx_device_id,
0176     .driver = {
0177         .name = "mc13xxx",
0178         .of_match_table = mc13xxx_dt_ids,
0179     },
0180     .probe = mc13xxx_spi_probe,
0181     .remove = mc13xxx_spi_remove,
0182 };
0183 
0184 static int __init mc13xxx_init(void)
0185 {
0186     return spi_register_driver(&mc13xxx_spi_driver);
0187 }
0188 subsys_initcall(mc13xxx_init);
0189 
0190 static void __exit mc13xxx_exit(void)
0191 {
0192     spi_unregister_driver(&mc13xxx_spi_driver);
0193 }
0194 module_exit(mc13xxx_exit);
0195 
0196 MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
0197 MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
0198 MODULE_LICENSE("GPL v2");