Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * NBUS driver for TS-4600 based boards
  4 *
  5 * Copyright (c) 2016 - Savoir-faire Linux
  6 * Author: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
  7 *
 
 
 
 
  8 * This driver implements a GPIOs bit-banged bus, called the NBUS by Technologic
  9 * Systems. It is used to communicate with the peripherals in the FPGA on the
 10 * TS-4600 SoM.
 11 */
 12
 13#include <linux/bitops.h>
 14#include <linux/gpio/consumer.h>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/mutex.h>
 18#include <linux/of_platform.h>
 19#include <linux/platform_device.h>
 20#include <linux/pwm.h>
 21#include <linux/ts-nbus.h>
 22
 23#define TS_NBUS_DIRECTION_IN  0
 24#define TS_NBUS_DIRECTION_OUT 1
 25#define TS_NBUS_WRITE_ADR 0
 26#define TS_NBUS_WRITE_VAL 1
 27
 28struct ts_nbus {
 29	struct pwm_device *pwm;
 30	struct gpio_descs *data;
 31	struct gpio_desc *csn;
 32	struct gpio_desc *txrx;
 33	struct gpio_desc *strobe;
 34	struct gpio_desc *ale;
 35	struct gpio_desc *rdy;
 36	struct mutex lock;
 37};
 38
 39/*
 40 * request all gpios required by the bus.
 41 */
 42static int ts_nbus_init_pdata(struct platform_device *pdev,
 43			      struct ts_nbus *ts_nbus)
 44{
 45	ts_nbus->data = devm_gpiod_get_array(&pdev->dev, "ts,data",
 46			GPIOD_OUT_HIGH);
 47	if (IS_ERR(ts_nbus->data))
 48		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->data),
 49				     "failed to retrieve ts,data-gpio from dts\n");
 
 50
 51	ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH);
 52	if (IS_ERR(ts_nbus->csn))
 53		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->csn),
 54			      "failed to retrieve ts,csn-gpio from dts\n");
 
 55
 56	ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH);
 57	if (IS_ERR(ts_nbus->txrx))
 58		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->txrx),
 59				     "failed to retrieve ts,txrx-gpio from dts\n");
 
 60
 61	ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH);
 62	if (IS_ERR(ts_nbus->strobe))
 63		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->strobe),
 64				     "failed to retrieve ts,strobe-gpio from dts\n");
 
 65
 66	ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH);
 67	if (IS_ERR(ts_nbus->ale))
 68		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->ale),
 69				     "failed to retrieve ts,ale-gpio from dts\n");
 
 70
 71	ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN);
 72	if (IS_ERR(ts_nbus->rdy))
 73		return dev_err_probe(&pdev->dev, PTR_ERR(ts_nbus->rdy),
 74				     "failed to retrieve ts,rdy-gpio from dts\n");
 
 75
 76	return 0;
 77}
 78
 79/*
 80 * the data gpios are used for reading and writing values, their directions
 81 * should be adjusted accordingly.
 82 */
 83static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
 84{
 85	int i;
 86
 87	for (i = 0; i < 8; i++) {
 88		if (direction == TS_NBUS_DIRECTION_IN)
 89			gpiod_direction_input(ts_nbus->data->desc[i]);
 90		else
 91			/* when used as output the default state of the data
 92			 * lines are set to high */
 93			gpiod_direction_output(ts_nbus->data->desc[i], 1);
 94	}
 95}
 96
 97/*
 98 * reset the bus in its initial state.
 99 * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a
100 * new transaction can be process.
101 */
102static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
103{
104	DECLARE_BITMAP(values, 8);
 
105
106	values[0] = 0;
 
107
108	gpiod_set_array_value_cansleep(8, ts_nbus->data->desc,
109				       ts_nbus->data->info, values);
110	gpiod_set_value_cansleep(ts_nbus->csn, 0);
111	gpiod_set_value_cansleep(ts_nbus->strobe, 0);
112	gpiod_set_value_cansleep(ts_nbus->ale, 0);
113}
114
115/*
116 * let the FPGA knows it can process.
117 */
118static void ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
119{
120	gpiod_set_value_cansleep(ts_nbus->strobe, 1);
121}
122
123/*
124 * read a byte value from the data gpios.
125 * return 0 on success or negative errno on failure.
126 */
127static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
128{
129	struct gpio_descs *gpios = ts_nbus->data;
130	int ret, i;
131
132	*val = 0;
133	for (i = 0; i < 8; i++) {
134		ret = gpiod_get_value_cansleep(gpios->desc[i]);
135		if (ret < 0)
136			return ret;
137		if (ret)
138			*val |= BIT(i);
139	}
140
141	return 0;
142}
143
144/*
145 * set the data gpios accordingly to the byte value.
146 */
147static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
148{
149	struct gpio_descs *gpios = ts_nbus->data;
150	DECLARE_BITMAP(values, 8);
 
151
152	values[0] = byte;
 
 
 
 
153
154	gpiod_set_array_value_cansleep(8, gpios->desc, gpios->info, values);
155}
156
157/*
158 * reading the bus consists of resetting the bus, then notifying the FPGA to
159 * send the data in the data gpios and return the read value.
160 * return 0 on success or negative errno on failure.
161 */
162static int ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
163{
164	ts_nbus_reset_bus(ts_nbus);
165	ts_nbus_start_transaction(ts_nbus);
166
167	return ts_nbus_read_byte(ts_nbus, val);
168}
169
170/*
171 * writing to the bus consists of resetting the bus, then define the type of
172 * command (address/value), write the data and notify the FPGA to retrieve the
173 * value in the data gpios.
174 */
175static void ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
176{
177	ts_nbus_reset_bus(ts_nbus);
178
179	if (cmd == TS_NBUS_WRITE_ADR)
180		gpiod_set_value_cansleep(ts_nbus->ale, 1);
181
182	ts_nbus_write_byte(ts_nbus, val);
183	ts_nbus_start_transaction(ts_nbus);
184}
185
186/*
187 * read the value in the FPGA register at the given address.
188 * return 0 on success or negative errno on failure.
189 */
190int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
191{
192	int ret, i;
193	u8 byte;
194
195	/* bus access must be atomic */
196	mutex_lock(&ts_nbus->lock);
197
198	/* set the bus in read mode */
199	gpiod_set_value_cansleep(ts_nbus->txrx, 0);
200
201	/* write address */
202	ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
203
204	/* set the data gpios direction as input before reading */
205	ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
206
207	/* reading value MSB first */
208	do {
209		*val = 0;
210		byte = 0;
211		for (i = 1; i >= 0; i--) {
212			/* read a byte from the bus, leave on error */
213			ret = ts_nbus_read_bus(ts_nbus, &byte);
214			if (ret < 0)
215				goto err;
216
217			/* append the byte read to the final value */
218			*val |= byte << (i * 8);
219		}
220		gpiod_set_value_cansleep(ts_nbus->csn, 1);
221		ret = gpiod_get_value_cansleep(ts_nbus->rdy);
222	} while (ret);
223
224err:
225	/* restore the data gpios direction as output after reading */
226	ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
227
228	mutex_unlock(&ts_nbus->lock);
229
230	return ret;
231}
232EXPORT_SYMBOL_GPL(ts_nbus_read);
233
234/*
235 * write the desired value in the FPGA register at the given address.
236 */
237int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
238{
239	int i;
240
241	/* bus access must be atomic */
242	mutex_lock(&ts_nbus->lock);
243
244	/* set the bus in write mode */
245	gpiod_set_value_cansleep(ts_nbus->txrx, 1);
246
247	/* write address */
248	ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
249
250	/* writing value MSB first */
251	for (i = 1; i >= 0; i--)
252		ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_VAL, (u8)(val >> (i * 8)));
253
254	/* wait for completion */
255	gpiod_set_value_cansleep(ts_nbus->csn, 1);
256	while (gpiod_get_value_cansleep(ts_nbus->rdy) != 0) {
257		gpiod_set_value_cansleep(ts_nbus->csn, 0);
258		gpiod_set_value_cansleep(ts_nbus->csn, 1);
259	}
260
261	mutex_unlock(&ts_nbus->lock);
262
263	return 0;
264}
265EXPORT_SYMBOL_GPL(ts_nbus_write);
266
267static int ts_nbus_probe(struct platform_device *pdev)
268{
269	struct pwm_device *pwm;
270	struct pwm_state state;
271	struct device *dev = &pdev->dev;
272	struct ts_nbus *ts_nbus;
273	int ret;
274
275	ts_nbus = devm_kzalloc(dev, sizeof(*ts_nbus), GFP_KERNEL);
276	if (!ts_nbus)
277		return -ENOMEM;
278
279	mutex_init(&ts_nbus->lock);
280
281	ret = ts_nbus_init_pdata(pdev, ts_nbus);
282	if (ret < 0)
283		return ret;
284
285	pwm = devm_pwm_get(dev, NULL);
286	if (IS_ERR(pwm))
287		return dev_err_probe(dev, PTR_ERR(pwm),
288				     "unable to request PWM\n");
289
290	pwm_init_state(pwm, &state);
291	if (!state.period)
292		return dev_err_probe(dev, -EINVAL, "invalid PWM period\n");
293
294	state.duty_cycle = state.period;
295	state.enabled = true;
 
 
 
296
297	ret = pwm_apply_might_sleep(pwm, &state);
 
 
 
 
 
298	if (ret < 0)
299		return dev_err_probe(dev, ret, "failed to configure PWM\n");
300
301	/*
302	 * we can now start the FPGA and populate the peripherals.
303	 */
 
304	ts_nbus->pwm = pwm;
305
306	/*
307	 * let the child nodes retrieve this instance of the ts-nbus.
308	 */
309	dev_set_drvdata(dev, ts_nbus);
310
311	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
312	if (ret < 0)
313		return dev_err_probe(dev, ret,
314				     "failed to populate platform devices on bus\n");
315
316	dev_info(dev, "initialized\n");
317
318	return 0;
319}
320
321static void ts_nbus_remove(struct platform_device *pdev)
322{
323	struct ts_nbus *ts_nbus = dev_get_drvdata(&pdev->dev);
324
325	/* shutdown the FPGA */
326	mutex_lock(&ts_nbus->lock);
327	pwm_disable(ts_nbus->pwm);
328	mutex_unlock(&ts_nbus->lock);
 
 
329}
330
331static const struct of_device_id ts_nbus_of_match[] = {
332	{ .compatible = "technologic,ts-nbus", },
333	{ },
334};
335MODULE_DEVICE_TABLE(of, ts_nbus_of_match);
336
337static struct platform_driver ts_nbus_driver = {
338	.probe		= ts_nbus_probe,
339	.remove		= ts_nbus_remove,
340	.driver		= {
341		.name	= "ts_nbus",
342		.of_match_table = ts_nbus_of_match,
343	},
344};
345
346module_platform_driver(ts_nbus_driver);
347
348MODULE_ALIAS("platform:ts_nbus");
349MODULE_AUTHOR("Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>");
350MODULE_DESCRIPTION("Technologic Systems NBUS");
351MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  2 * NBUS driver for TS-4600 based boards
  3 *
  4 * Copyright (c) 2016 - Savoir-faire Linux
  5 * Author: Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>
  6 *
  7 * This file is licensed under the terms of the GNU General Public
  8 * License version 2. This program is licensed "as is" without any
  9 * warranty of any kind, whether express or implied.
 10 *
 11 * This driver implements a GPIOs bit-banged bus, called the NBUS by Technologic
 12 * Systems. It is used to communicate with the peripherals in the FPGA on the
 13 * TS-4600 SoM.
 14 */
 15
 16#include <linux/bitops.h>
 17#include <linux/gpio/consumer.h>
 18#include <linux/kernel.h>
 19#include <linux/module.h>
 20#include <linux/mutex.h>
 21#include <linux/of_platform.h>
 22#include <linux/platform_device.h>
 23#include <linux/pwm.h>
 24#include <linux/ts-nbus.h>
 25
 26#define TS_NBUS_DIRECTION_IN  0
 27#define TS_NBUS_DIRECTION_OUT 1
 28#define TS_NBUS_WRITE_ADR 0
 29#define TS_NBUS_WRITE_VAL 1
 30
 31struct ts_nbus {
 32	struct pwm_device *pwm;
 33	struct gpio_descs *data;
 34	struct gpio_desc *csn;
 35	struct gpio_desc *txrx;
 36	struct gpio_desc *strobe;
 37	struct gpio_desc *ale;
 38	struct gpio_desc *rdy;
 39	struct mutex lock;
 40};
 41
 42/*
 43 * request all gpios required by the bus.
 44 */
 45static int ts_nbus_init_pdata(struct platform_device *pdev, struct ts_nbus
 46		*ts_nbus)
 47{
 48	ts_nbus->data = devm_gpiod_get_array(&pdev->dev, "ts,data",
 49			GPIOD_OUT_HIGH);
 50	if (IS_ERR(ts_nbus->data)) {
 51		dev_err(&pdev->dev, "failed to retrieve ts,data-gpio from dts\n");
 52		return PTR_ERR(ts_nbus->data);
 53	}
 54
 55	ts_nbus->csn = devm_gpiod_get(&pdev->dev, "ts,csn", GPIOD_OUT_HIGH);
 56	if (IS_ERR(ts_nbus->csn)) {
 57		dev_err(&pdev->dev, "failed to retrieve ts,csn-gpio from dts\n");
 58		return PTR_ERR(ts_nbus->csn);
 59	}
 60
 61	ts_nbus->txrx = devm_gpiod_get(&pdev->dev, "ts,txrx", GPIOD_OUT_HIGH);
 62	if (IS_ERR(ts_nbus->txrx)) {
 63		dev_err(&pdev->dev, "failed to retrieve ts,txrx-gpio from dts\n");
 64		return PTR_ERR(ts_nbus->txrx);
 65	}
 66
 67	ts_nbus->strobe = devm_gpiod_get(&pdev->dev, "ts,strobe", GPIOD_OUT_HIGH);
 68	if (IS_ERR(ts_nbus->strobe)) {
 69		dev_err(&pdev->dev, "failed to retrieve ts,strobe-gpio from dts\n");
 70		return PTR_ERR(ts_nbus->strobe);
 71	}
 72
 73	ts_nbus->ale = devm_gpiod_get(&pdev->dev, "ts,ale", GPIOD_OUT_HIGH);
 74	if (IS_ERR(ts_nbus->ale)) {
 75		dev_err(&pdev->dev, "failed to retrieve ts,ale-gpio from dts\n");
 76		return PTR_ERR(ts_nbus->ale);
 77	}
 78
 79	ts_nbus->rdy = devm_gpiod_get(&pdev->dev, "ts,rdy", GPIOD_IN);
 80	if (IS_ERR(ts_nbus->rdy)) {
 81		dev_err(&pdev->dev, "failed to retrieve ts,rdy-gpio from dts\n");
 82		return PTR_ERR(ts_nbus->rdy);
 83	}
 84
 85	return 0;
 86}
 87
 88/*
 89 * the data gpios are used for reading and writing values, their directions
 90 * should be adjusted accordingly.
 91 */
 92static void ts_nbus_set_direction(struct ts_nbus *ts_nbus, int direction)
 93{
 94	int i;
 95
 96	for (i = 0; i < 8; i++) {
 97		if (direction == TS_NBUS_DIRECTION_IN)
 98			gpiod_direction_input(ts_nbus->data->desc[i]);
 99		else
100			/* when used as output the default state of the data
101			 * lines are set to high */
102			gpiod_direction_output(ts_nbus->data->desc[i], 1);
103	}
104}
105
106/*
107 * reset the bus in its initial state.
108 * The data, csn, strobe and ale lines must be zero'ed to let the FPGA knows a
109 * new transaction can be process.
110 */
111static void ts_nbus_reset_bus(struct ts_nbus *ts_nbus)
112{
113	int i;
114	int values[8];
115
116	for (i = 0; i < 8; i++)
117		values[i] = 0;
118
119	gpiod_set_array_value_cansleep(8, ts_nbus->data->desc, values);
 
120	gpiod_set_value_cansleep(ts_nbus->csn, 0);
121	gpiod_set_value_cansleep(ts_nbus->strobe, 0);
122	gpiod_set_value_cansleep(ts_nbus->ale, 0);
123}
124
125/*
126 * let the FPGA knows it can process.
127 */
128static void ts_nbus_start_transaction(struct ts_nbus *ts_nbus)
129{
130	gpiod_set_value_cansleep(ts_nbus->strobe, 1);
131}
132
133/*
134 * read a byte value from the data gpios.
135 * return 0 on success or negative errno on failure.
136 */
137static int ts_nbus_read_byte(struct ts_nbus *ts_nbus, u8 *val)
138{
139	struct gpio_descs *gpios = ts_nbus->data;
140	int ret, i;
141
142	*val = 0;
143	for (i = 0; i < 8; i++) {
144		ret = gpiod_get_value_cansleep(gpios->desc[i]);
145		if (ret < 0)
146			return ret;
147		if (ret)
148			*val |= BIT(i);
149	}
150
151	return 0;
152}
153
154/*
155 * set the data gpios accordingly to the byte value.
156 */
157static void ts_nbus_write_byte(struct ts_nbus *ts_nbus, u8 byte)
158{
159	struct gpio_descs *gpios = ts_nbus->data;
160	int i;
161	int values[8];
162
163	for (i = 0; i < 8; i++)
164		if (byte & BIT(i))
165			values[i] = 1;
166		else
167			values[i] = 0;
168
169	gpiod_set_array_value_cansleep(8, gpios->desc, values);
170}
171
172/*
173 * reading the bus consists of resetting the bus, then notifying the FPGA to
174 * send the data in the data gpios and return the read value.
175 * return 0 on success or negative errno on failure.
176 */
177static int ts_nbus_read_bus(struct ts_nbus *ts_nbus, u8 *val)
178{
179	ts_nbus_reset_bus(ts_nbus);
180	ts_nbus_start_transaction(ts_nbus);
181
182	return ts_nbus_read_byte(ts_nbus, val);
183}
184
185/*
186 * writing to the bus consists of resetting the bus, then define the type of
187 * command (address/value), write the data and notify the FPGA to retrieve the
188 * value in the data gpios.
189 */
190static void ts_nbus_write_bus(struct ts_nbus *ts_nbus, int cmd, u8 val)
191{
192	ts_nbus_reset_bus(ts_nbus);
193
194	if (cmd == TS_NBUS_WRITE_ADR)
195		gpiod_set_value_cansleep(ts_nbus->ale, 1);
196
197	ts_nbus_write_byte(ts_nbus, val);
198	ts_nbus_start_transaction(ts_nbus);
199}
200
201/*
202 * read the value in the FPGA register at the given address.
203 * return 0 on success or negative errno on failure.
204 */
205int ts_nbus_read(struct ts_nbus *ts_nbus, u8 adr, u16 *val)
206{
207	int ret, i;
208	u8 byte;
209
210	/* bus access must be atomic */
211	mutex_lock(&ts_nbus->lock);
212
213	/* set the bus in read mode */
214	gpiod_set_value_cansleep(ts_nbus->txrx, 0);
215
216	/* write address */
217	ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
218
219	/* set the data gpios direction as input before reading */
220	ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_IN);
221
222	/* reading value MSB first */
223	do {
224		*val = 0;
225		byte = 0;
226		for (i = 1; i >= 0; i--) {
227			/* read a byte from the bus, leave on error */
228			ret = ts_nbus_read_bus(ts_nbus, &byte);
229			if (ret < 0)
230				goto err;
231
232			/* append the byte read to the final value */
233			*val |= byte << (i * 8);
234		}
235		gpiod_set_value_cansleep(ts_nbus->csn, 1);
236		ret = gpiod_get_value_cansleep(ts_nbus->rdy);
237	} while (ret);
238
239err:
240	/* restore the data gpios direction as output after reading */
241	ts_nbus_set_direction(ts_nbus, TS_NBUS_DIRECTION_OUT);
242
243	mutex_unlock(&ts_nbus->lock);
244
245	return ret;
246}
247EXPORT_SYMBOL_GPL(ts_nbus_read);
248
249/*
250 * write the desired value in the FPGA register at the given address.
251 */
252int ts_nbus_write(struct ts_nbus *ts_nbus, u8 adr, u16 val)
253{
254	int i;
255
256	/* bus access must be atomic */
257	mutex_lock(&ts_nbus->lock);
258
259	/* set the bus in write mode */
260	gpiod_set_value_cansleep(ts_nbus->txrx, 1);
261
262	/* write address */
263	ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_ADR, adr);
264
265	/* writing value MSB first */
266	for (i = 1; i >= 0; i--)
267		ts_nbus_write_bus(ts_nbus, TS_NBUS_WRITE_VAL, (u8)(val >> (i * 8)));
268
269	/* wait for completion */
270	gpiod_set_value_cansleep(ts_nbus->csn, 1);
271	while (gpiod_get_value_cansleep(ts_nbus->rdy) != 0) {
272		gpiod_set_value_cansleep(ts_nbus->csn, 0);
273		gpiod_set_value_cansleep(ts_nbus->csn, 1);
274	}
275
276	mutex_unlock(&ts_nbus->lock);
277
278	return 0;
279}
280EXPORT_SYMBOL_GPL(ts_nbus_write);
281
282static int ts_nbus_probe(struct platform_device *pdev)
283{
284	struct pwm_device *pwm;
285	struct pwm_args pargs;
286	struct device *dev = &pdev->dev;
287	struct ts_nbus *ts_nbus;
288	int ret;
289
290	ts_nbus = devm_kzalloc(dev, sizeof(*ts_nbus), GFP_KERNEL);
291	if (!ts_nbus)
292		return -ENOMEM;
293
294	mutex_init(&ts_nbus->lock);
295
296	ret = ts_nbus_init_pdata(pdev, ts_nbus);
297	if (ret < 0)
298		return ret;
299
300	pwm = devm_pwm_get(dev, NULL);
301	if (IS_ERR(pwm)) {
302		ret = PTR_ERR(pwm);
303		if (ret != -EPROBE_DEFER)
304			dev_err(dev, "unable to request PWM\n");
305		return ret;
306	}
 
307
308	pwm_get_args(pwm, &pargs);
309	if (!pargs.period) {
310		dev_err(&pdev->dev, "invalid PWM period\n");
311		return -EINVAL;
312	}
313
314	/*
315	 * FIXME: pwm_apply_args() should be removed when switching to
316	 * the atomic PWM API.
317	 */
318	pwm_apply_args(pwm);
319	ret = pwm_config(pwm, pargs.period, pargs.period);
320	if (ret < 0)
321		return ret;
322
323	/*
324	 * we can now start the FPGA and populate the peripherals.
325	 */
326	pwm_enable(pwm);
327	ts_nbus->pwm = pwm;
328
329	/*
330	 * let the child nodes retrieve this instance of the ts-nbus.
331	 */
332	dev_set_drvdata(dev, ts_nbus);
333
334	ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
335	if (ret < 0)
336		return ret;
 
337
338	dev_info(dev, "initialized\n");
339
340	return 0;
341}
342
343static int ts_nbus_remove(struct platform_device *pdev)
344{
345	struct ts_nbus *ts_nbus = dev_get_drvdata(&pdev->dev);
346
347	/* shutdown the FPGA */
348	mutex_lock(&ts_nbus->lock);
349	pwm_disable(ts_nbus->pwm);
350	mutex_unlock(&ts_nbus->lock);
351
352	return 0;
353}
354
355static const struct of_device_id ts_nbus_of_match[] = {
356	{ .compatible = "technologic,ts-nbus", },
357	{ },
358};
359MODULE_DEVICE_TABLE(of, ts_nbus_of_match);
360
361static struct platform_driver ts_nbus_driver = {
362	.probe		= ts_nbus_probe,
363	.remove		= ts_nbus_remove,
364	.driver		= {
365		.name	= "ts_nbus",
366		.of_match_table = ts_nbus_of_match,
367	},
368};
369
370module_platform_driver(ts_nbus_driver);
371
372MODULE_ALIAS("platform:ts_nbus");
373MODULE_AUTHOR("Sebastien Bourdelin <sebastien.bourdelin@savoirfairelinux.com>");
374MODULE_DESCRIPTION("Technologic Systems NBUS");
375MODULE_LICENSE("GPL v2");