blob: 5f14ef6693c22abd073b62ea6114dab2b09d76b8 [file] [log] [blame]
Marc Reillya0c7c1d2012-04-01 16:41:38 +10001/*
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>
16#include <linux/mutex.h>
17#include <linux/interrupt.h>
18#include <linux/mfd/core.h>
19#include <linux/mfd/mc13xxx.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/of_gpio.h>
23#include <linux/err.h>
24#include <linux/spi/spi.h>
25
26#include "mc13xxx.h"
27
28static const struct spi_device_id mc13xxx_device_id[] = {
29 {
30 .name = "mc13783",
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +000031 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13783,
Marc Reillya0c7c1d2012-04-01 16:41:38 +100032 }, {
33 .name = "mc13892",
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +000034 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
Marc Reillya0c7c1d2012-04-01 16:41:38 +100035 }, {
Uwe Kleine-König0312e022012-07-12 09:57:53 +000036 .name = "mc34708",
37 .driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
Marc Reillya0c7c1d2012-04-01 16:41:38 +100038 }, {
39 /* sentinel */
40 }
41};
42MODULE_DEVICE_TABLE(spi, mc13xxx_device_id);
43
44static const struct of_device_id mc13xxx_dt_ids[] = {
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +000045 { .compatible = "fsl,mc13783", .data = &mc13xxx_variant_mc13783, },
46 { .compatible = "fsl,mc13892", .data = &mc13xxx_variant_mc13892, },
Uwe Kleine-König0312e022012-07-12 09:57:53 +000047 { .compatible = "fsl,mc34708", .data = &mc13xxx_variant_mc34708, },
Marc Reillya0c7c1d2012-04-01 16:41:38 +100048 { /* sentinel */ }
49};
50MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
51
52static struct regmap_config mc13xxx_regmap_spi_config = {
53 .reg_bits = 7,
54 .pad_bits = 1,
55 .val_bits = 24,
Philippe Rétornaz77a5b372012-05-29 11:06:28 +020056 .write_flag_mask = 0x80,
Marc Reillya0c7c1d2012-04-01 16:41:38 +100057
58 .max_register = MC13XXX_NUMREGS,
59
60 .cache_type = REGCACHE_NONE,
Philippe Rétornaze4ecf6e2012-05-29 11:06:29 +020061 .use_single_rw = 1,
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);
Mark Brownfd792f82013-09-23 19:14:32 +010097 const char *reg = data;
Philippe Rétornaze4ecf6e2012-05-29 11:06:29 +020098
99 if (count != 4)
100 return -ENOTSUPP;
101
Mark Brownfd792f82013-09-23 19:14:32 +0100102 /* include errata fix for spi audio problems */
103 if (*reg == MC13783_AUDIO_CODEC || *reg == MC13783_AUDIO_DAC)
104 spi_write(spi, data, count);
105
Philippe Rétornaze4ecf6e2012-05-29 11:06:29 +0200106 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,
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000127};
128
129static int mc13xxx_spi_probe(struct spi_device *spi)
130{
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000131 struct mc13xxx *mc13xxx;
132 struct mc13xxx_platform_data *pdata = dev_get_platdata(&spi->dev);
133 int ret;
134
Axel Line7c706b2012-06-29 15:14:36 +0200135 mc13xxx = devm_kzalloc(&spi->dev, sizeof(*mc13xxx), GFP_KERNEL);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000136 if (!mc13xxx)
137 return -ENOMEM;
138
Jingoo Hana720c302013-04-06 15:44:14 +0900139 spi_set_drvdata(spi, mc13xxx);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000140 spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000141
142 mc13xxx->dev = &spi->dev;
143 mutex_init(&mc13xxx->lock);
144
Axel Line0308892012-07-02 16:03:00 +0800145 mc13xxx->regmap = devm_regmap_init(&spi->dev, &regmap_mc13xxx_bus,
146 &spi->dev,
147 &mc13xxx_regmap_spi_config);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000148 if (IS_ERR(mc13xxx->regmap)) {
149 ret = PTR_ERR(mc13xxx->regmap);
150 dev_err(mc13xxx->dev, "Failed to initialize register map: %d\n",
151 ret);
Jingoo Hana720c302013-04-06 15:44:14 +0900152 spi_set_drvdata(spi, NULL);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000153 return ret;
154 }
155
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000156 if (spi->dev.of_node) {
157 const struct of_device_id *of_id =
158 of_match_device(mc13xxx_dt_ids, &spi->dev);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000159
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000160 mc13xxx->variant = of_id->data;
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000161 } else {
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000162 const struct spi_device_id *id_entry = spi_get_device_id(spi);
163
164 mc13xxx->variant = (void *)id_entry->driver_data;
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000165 }
166
Uwe Kleine-Königcd0f34b2012-07-12 09:57:52 +0000167 return mc13xxx_common_init(mc13xxx, pdata, spi->irq);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000168}
169
Bill Pemberton4740f732012-11-19 13:26:01 -0500170static int mc13xxx_spi_remove(struct spi_device *spi)
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000171{
Jingoo Hana720c302013-04-06 15:44:14 +0900172 struct mc13xxx *mc13xxx = spi_get_drvdata(spi);
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000173
174 mc13xxx_common_cleanup(mc13xxx);
175
176 return 0;
177}
178
179static struct spi_driver mc13xxx_spi_driver = {
180 .id_table = mc13xxx_device_id,
181 .driver = {
182 .name = "mc13xxx",
183 .owner = THIS_MODULE,
184 .of_match_table = mc13xxx_dt_ids,
185 },
186 .probe = mc13xxx_spi_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500187 .remove = mc13xxx_spi_remove,
Marc Reillya0c7c1d2012-04-01 16:41:38 +1000188};
189
190static int __init mc13xxx_init(void)
191{
192 return spi_register_driver(&mc13xxx_spi_driver);
193}
194subsys_initcall(mc13xxx_init);
195
196static void __exit mc13xxx_exit(void)
197{
198 spi_unregister_driver(&mc13xxx_spi_driver);
199}
200module_exit(mc13xxx_exit);
201
202MODULE_DESCRIPTION("Core driver for Freescale MC13XXX PMIC");
203MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
204MODULE_LICENSE("GPL v2");