Linux Audio

Check our new training course

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