Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2009-2010 Pengutronix |
| 4 | * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de> |
| 5 | * |
| 6 | * loosely based on an earlier driver that has |
| 7 | * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/platform_device.h> |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/mfd/core.h> |
| 15 | #include <linux/mfd/mc13xxx.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/of_gpio.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/spi/spi.h> |
| 21 | |
| 22 | #include "mc13xxx.h" |
| 23 | |
| 24 | static const struct spi_device_id mc13xxx_device_id[] = { |
| 25 | { |
| 26 | .name = "mc13783", |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 27 | .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 28 | }, { |
| 29 | .name = "mc13892", |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 30 | .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 31 | }, { |
Uwe Kleine-König | 0312e02 | 2012-07-12 09:57:53 +0000 | [diff] [blame] | 32 | .name = "mc34708", |
| 33 | .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 34 | }, { |
| 35 | /* sentinel */ |
| 36 | } |
| 37 | }; |
| 38 | MODULE_DEVICE_TABLE(spi, mc13xxx_device_id); |
| 39 | |
| 40 | static const struct of_device_id mc13xxx_dt_ids[] = { |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 41 | { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, }, |
| 42 | { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, }, |
Uwe Kleine-König | 0312e02 | 2012-07-12 09:57:53 +0000 | [diff] [blame] | 43 | { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, }, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 44 | { /* sentinel */ } |
| 45 | }; |
| 46 | MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids); |
| 47 | |
Krzysztof Kozlowski | 18dd21a | 2015-01-05 10:01:26 +0100 | [diff] [blame] | 48 | static const struct regmap_config mc13xxx_regmap_spi_config = { |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 49 | .reg_bits = 7, |
| 50 | .pad_bits = 1, |
| 51 | .val_bits = 24, |
Philippe Rétornaz | 77a5b37 | 2012-05-29 11:06:28 +0200 | [diff] [blame] | 52 | .write_flag_mask = 0x80, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 53 | |
| 54 | .max_register = MC13XXX_NUMREGS, |
| 55 | |
| 56 | .cache_type = REGCACHE_NONE, |
David Frey | 1c96a2f | 2018-09-01 09:50:41 -0700 | [diff] [blame] | 57 | .use_single_read = true, |
| 58 | .use_single_write = true, |
Philippe Rétornaz | e4ecf6e | 2012-05-29 11:06:29 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size, |
| 62 | void *val, size_t val_size) |
| 63 | { |
| 64 | unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0}; |
| 65 | unsigned char r[4]; |
| 66 | unsigned char *p = val; |
| 67 | struct device *dev = context; |
| 68 | struct spi_device *spi = to_spi_device(dev); |
| 69 | struct spi_transfer t = { |
| 70 | .tx_buf = w, |
| 71 | .rx_buf = r, |
| 72 | .len = 4, |
| 73 | }; |
| 74 | |
| 75 | struct spi_message m; |
| 76 | int ret; |
| 77 | |
| 78 | if (val_size != 3 || reg_size != 1) |
| 79 | return -ENOTSUPP; |
| 80 | |
| 81 | spi_message_init(&m); |
| 82 | spi_message_add_tail(&t, &m); |
| 83 | ret = spi_sync(spi, &m); |
| 84 | |
| 85 | memcpy(p, &r[1], 3); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static int mc13xxx_spi_write(void *context, const void *data, size_t count) |
| 91 | { |
| 92 | struct device *dev = context; |
| 93 | struct spi_device *spi = to_spi_device(dev); |
Mark Brown | fd792f8 | 2013-09-23 19:14:32 +0100 | [diff] [blame] | 94 | const char *reg = data; |
Philippe Rétornaz | e4ecf6e | 2012-05-29 11:06:29 +0200 | [diff] [blame] | 95 | |
| 96 | if (count != 4) |
| 97 | return -ENOTSUPP; |
| 98 | |
Mark Brown | fd792f8 | 2013-09-23 19:14:32 +0100 | [diff] [blame] | 99 | /* include errata fix for spi audio problems */ |
| 100 | if (*reg == MC13783_AUDIO_CODEC || *reg == MC13783_AUDIO_DAC) |
| 101 | spi_write(spi, data, count); |
| 102 | |
Philippe Rétornaz | e4ecf6e | 2012-05-29 11:06:29 +0200 | [diff] [blame] | 103 | return spi_write(spi, data, count); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * We cannot use regmap-spi generic bus implementation here. |
| 108 | * The MC13783 chip will get corrupted if CS signal is deasserted |
| 109 | * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller |
| 110 | * has the following errata (DSPhl22960): |
| 111 | * "The CSPI negates SS when the FIFO becomes empty with |
| 112 | * SSCTL= 0. Software cannot guarantee that the FIFO will not |
| 113 | * drain because of higher priority interrupts and the |
| 114 | * non-realtime characteristics of the operating system. As a |
| 115 | * result, the SS will negate before all of the data has been |
| 116 | * transferred to/from the peripheral." |
| 117 | * We workaround this by accessing the SPI controller with a |
| 118 | * single transfert. |
| 119 | */ |
| 120 | |
| 121 | static struct regmap_bus regmap_mc13xxx_bus = { |
| 122 | .write = mc13xxx_spi_write, |
| 123 | .read = mc13xxx_spi_read, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static int mc13xxx_spi_probe(struct spi_device *spi) |
| 127 | { |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 128 | struct mc13xxx *mc13xxx; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 129 | int ret; |
| 130 | |
Axel Lin | e7c706b | 2012-06-29 15:14:36 +0200 | [diff] [blame] | 131 | mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL); |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 132 | if (!mc13xxx) |
| 133 | return -ENOMEM; |
| 134 | |
Alexander Shiyan | db9ef44 | 2013-12-14 17:03:12 +0400 | [diff] [blame] | 135 | dev_set_drvdata(&spi->dev, mc13xxx); |
| 136 | |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 137 | spi->mode = SPI_MODE_0 | SPI_CS_HIGH; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 138 | |
Alexander Shiyan | db9ef44 | 2013-12-14 17:03:12 +0400 | [diff] [blame] | 139 | mc13xxx->irq = spi->irq; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 140 | |
Alexander Shiyan | 0cfe5c9 | 2014-03-02 11:44:33 +0400 | [diff] [blame] | 141 | spi->max_speed_hz = spi->max_speed_hz ? : 26000000; |
Alexander Shiyan | 6a5926e | 2014-03-02 11:44:34 +0400 | [diff] [blame] | 142 | ret = spi_setup(spi); |
| 143 | if (ret) |
| 144 | return ret; |
Alexander Shiyan | 0cfe5c9 | 2014-03-02 11:44:33 +0400 | [diff] [blame] | 145 | |
Axel Lin | e030889 | 2012-07-02 16:03:00 +0800 | [diff] [blame] | 146 | mc13xxx->regmap = devm_regmap_init(&spi->dev, ®map_mc13xxx_bus, |
| 147 | &spi->dev, |
| 148 | &mc13xxx_regmap_spi_config); |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 149 | if (IS_ERR(mc13xxx->regmap)) { |
| 150 | ret = PTR_ERR(mc13xxx->regmap); |
Alexander Shiyan | db9ef44 | 2013-12-14 17:03:12 +0400 | [diff] [blame] | 151 | dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret); |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 155 | if (spi->dev.of_node) { |
| 156 | const struct of_device_id *of_id = |
| 157 | of_match_device(mc13xxx_dt_ids, &spi->dev); |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 158 | |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 159 | mc13xxx->variant = of_id->data; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 160 | } else { |
Uwe Kleine-König | cd0f34b | 2012-07-12 09:57:52 +0000 | [diff] [blame] | 161 | const struct spi_device_id *id_entry = spi_get_device_id(spi); |
| 162 | |
| 163 | mc13xxx->variant = (void *)id_entry->driver_data; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Shiyan | db9ef44 | 2013-12-14 17:03:12 +0400 | [diff] [blame] | 166 | return mc13xxx_common_init(&spi->dev); |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 167 | } |
| 168 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 169 | static int mc13xxx_spi_remove(struct spi_device *spi) |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 170 | { |
Uwe Kleine-König | c39cf60 | 2021-10-12 17:39:33 +0200 | [diff] [blame] | 171 | mc13xxx_common_exit(&spi->dev); |
| 172 | return 0; |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static struct spi_driver mc13xxx_spi_driver = { |
| 176 | .id_table = mc13xxx_device_id, |
| 177 | .driver = { |
| 178 | .name = "mc13xxx", |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 179 | .of_match_table = mc13xxx_dt_ids, |
| 180 | }, |
| 181 | .probe = mc13xxx_spi_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 182 | .remove = mc13xxx_spi_remove, |
Marc Reilly | a0c7c1d | 2012-04-01 16:41:38 +1000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static int __init mc13xxx_init(void) |
| 186 | { |
| 187 | return spi_register_driver(&mc13xxx_spi_driver); |
| 188 | } |
| 189 | subsys_initcall(mc13xxx_init); |
| 190 | |
| 191 | static void __exit mc13xxx_exit(void) |
| 192 | { |
| 193 | spi_unregister_driver(&mc13xxx_spi_driver); |
| 194 | } |
| 195 | module_exit(mc13xxx_exit); |
| 196 | |
| 197 | MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC"); |
| 198 | MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>"); |
| 199 | MODULE_LICENSE("GPL v2"); |