Merge tag 'omap-for-v5.0/fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / mfd / mc13xxx-spi.c
CommitLineData
a0c7c1d4
MR
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>
a0c7c1d4
MR
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
27static const struct spi_device_id mc13xxx_device_id[] = {
28 {
29 .name = "mc13783",
cd0f34b0 30 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783,
a0c7c1d4
MR
31 }, {
32 .name = "mc13892",
cd0f34b0 33 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
0312e024
UKK
34 }, {
35 .name = "mc34708",
36 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
a0c7c1d4
MR
37 }, {
38 /* sentinel */
39 }
40};
41MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
42
43static const struct of_device_id mc13xxx_dt_ids[] = {
cd0f34b0
UKK
44 { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, },
45 { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, },
0312e024 46 { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, },
a0c7c1d4
MR
47 { /* sentinel */ }
48};
49MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
50
18dd21ab 51static const struct regmap_config mc13xxx_regmap_spi_config = {
a0c7c1d4
MR
52 .reg_bits = 7,
53 .pad_bits = 1,
54 .val_bits = 24,
77a5b370 55 .write_flag_mask = 0x80,
a0c7c1d4
MR
56
57 .max_register = MC13XXX_NUMREGS,
58
59 .cache_type = REGCACHE_NONE,
1c96a2f6
DF
60 .use_single_read = true,
61 .use_single_write = true,
e4ecf6ea
PR
62};
63
64static int mc13xxx_spi_read(void *context, const void *reg, size_t reg_size,
65 void *val, size_t val_size)
66{
67 unsigned char w[4] = { *((unsigned char *) reg), 0, 0, 0};
68 unsigned char r[4];
69 unsigned char *p = val;
70 struct device *dev = context;
71 struct spi_device *spi = to_spi_device(dev);
72 struct spi_transfer t = {
73 .tx_buf = w,
74 .rx_buf = r,
75 .len = 4,
76 };
77
78 struct spi_message m;
79 int ret;
80
81 if (val_size != 3 || reg_size != 1)
82 return -ENOTSUPP;
83
84 spi_message_init(&m);
85 spi_message_add_tail(&t, &m);
86 ret = spi_sync(spi, &m);
87
88 memcpy(p, &r[1], 3);
89
90 return ret;
91}
92
93static int mc13xxx_spi_write(void *context, const void *data, size_t count)
94{
95 struct device *dev = context;
96 struct spi_device *spi = to_spi_device(dev);
fd792f8f 97 const char *reg = data;
e4ecf6ea
PR
98
99 if (count != 4)
100 return -ENOTSUPP;
101
fd792f8f
MB
102 /* include errata fix for spi audio problems */
103 if (*reg == MC13783_AUDIO_CODEC || *reg == MC13783_AUDIO_DAC)
104 spi_write(spi, data, count);
105
e4ecf6ea
PR
106 return spi_write(spi, data, count);
107}
108
109/*
110 * We cannot use regmap-spi generic bus implementation here.
111 * The MC13783 chip will get corrupted if CS signal is deasserted
112 * and on i.Mx31 SoC (the target SoC for MC13783 PMIC) the SPI controller
113 * has the following errata (DSPhl22960):
114 * "The CSPI negates SS when the FIFO becomes empty with
115 * SSCTL= 0. Software cannot guarantee that the FIFO will not
116 * drain because of higher priority interrupts and the
117 * non-realtime characteristics of the operating system. As a
118 * result, the SS will negate before all of the data has been
119 * transferred to/from the peripheral."
120 * We workaround this by accessing the SPI controller with a
121 * single transfert.
122 */
123
124static struct regmap_bus regmap_mc13xxx_bus = {
125 .write = mc13xxx_spi_write,
126 .read = mc13xxx_spi_read,
a0c7c1d4
MR
127};
128
129static int mc13xxx_spi_probe(struct spi_device *spi)
130{
a0c7c1d4 131 struct mc13xxx *mc13xxx;
a0c7c1d4
MR
132 int ret;
133
e7c706b1 134 mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL);
a0c7c1d4
MR
135 if (!mc13xxx)
136 return -ENOMEM;
137
db9ef449
AS
138 dev_set_drvdata(&spi->dev, mc13xxx);
139
a0c7c1d4 140 spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
a0c7c1d4 141
db9ef449 142 mc13xxx->irq = spi->irq;
a0c7c1d4 143
0cfe5c90 144 spi->max_speed_hz = spi->max_speed_hz ? : 26000000;
6a5926e6
AS
145 ret = spi_setup(spi);
146 if (ret)
147 return ret;
0cfe5c90 148
e0308897
AL
149 mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
150 &spi->dev,
151 &mc13xxx_regmap_spi_config);
a0c7c1d4
MR
152 if (IS_ERR(mc13xxx->regmap)) {
153 ret = PTR_ERR(mc13xxx->regmap);
db9ef449 154 dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret);
a0c7c1d4
MR
155 return ret;
156 }
157
cd0f34b0
UKK
158 if (spi->dev.of_node) {
159 const struct of_device_id *of_id =
160 of_match_device(mc13xxx_dt_ids, &spi->dev);
a0c7c1d4 161
cd0f34b0 162 mc13xxx->variant = of_id->data;
a0c7c1d4 163 } else {
cd0f34b0
UKK
164 const struct spi_device_id *id_entry = spi_get_device_id(spi);
165
166 mc13xxx->variant = (void *)id_entry->driver_data;
a0c7c1d4
MR
167 }
168
db9ef449 169 return mc13xxx_common_init(&spi->dev);
a0c7c1d4
MR
170}
171
4740f73f 172static int mc13xxx_spi_remove(struct spi_device *spi)
a0c7c1d4 173{
db9ef449 174 return mc13xxx_common_exit(&spi->dev);
a0c7c1d4
MR
175}
176
177static struct spi_driver mc13xxx_spi_driver = {
178 .id_table = mc13xxx_device_id,
179 .driver = {
180 .name = "mc13xxx",
a0c7c1d4
MR
181 .of_match_table = mc13xxx_dt_ids,
182 },
183 .probe = mc13xxx_spi_probe,
84449216 184 .remove = mc13xxx_spi_remove,
a0c7c1d4
MR
185};
186
187static int __init mc13xxx_init(void)
188{
189 return spi_register_driver(&mc13xxx_spi_driver);
190}
191subsys_initcall(mc13xxx_init);
192
193static void __exit mc13xxx_exit(void)
194{
195 spi_unregister_driver(&mc13xxx_spi_driver);
196}
197module_exit(mc13xxx_exit);
198
199MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
200MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
201MODULE_LICENSE("GPL v2");