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