summaryrefslogtreecommitdiff
path: root/drivers/pwm/pwm-rpmsg-imx.c
blob: c1b7c1123e622bc5af7033c0679444915add4242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2021 NXP.
 */

/*
 * The pwm-rpmsg transfer protocol:
 *   +---------------+-------------------------------+
 *   |  Byte Offset  |            Content            |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       0       |           Category            |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |     1 ~ 2     |           Version             |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       3       |             Type              |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       4       |           Command             |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       5       |           Reserved0           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       6       |           Reserved1           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       7       |           Reserved2           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       8       |           Reserved3           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       9       |           Reserved4           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       10      |          Request ID           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       11      |         Return Value          |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       12      |            PWM ID             |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       13      |          Channel ID           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |    14 ~ 21    |            Period             |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |    22 ~ 29    |          Duty Cycle           |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       30      |           Polarity            |
 *   +---------------+---+---+---+---+---+---+---+---+
 *   |       31      |            Enable             |
 *   +---------------+---+---+---+---+---+---+---+---+
 *
 * The definition of Return Value:
 * 0x00 = Success
 * 0x01 = Failed
 * 0x02 = Invalid parameter
 * 0x03 = Chip Busy
 * 0x04 = Operate in invalid state
 * 0x05 = Memory allocation failed
 * 0x06 = Timeout when waiting for an event
 * 0x07 = Cannot add to list as node already in another list
 * 0x08 = Cannot remove from list as node not in list
 * 0x09 = Transfer timeout
 * 0x0A = Transfer failed due to peer core not ready
 * 0x0B = Transfer failed due to communication failure
 * 0x0C = Cannot find service for a request/notification
 * 0x0D = Service version cannot support the request/notification
 */

#include <linux/init.h>
#include <linux/io.h>
#include <linux/imx_rpmsg.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/pwm.h>
#include <linux/platform_device.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/rpmsg.h>

#define PWM_RPMSG_TIMEOUT_MS			500

#define PWM_RPMSG_CATEGORY			0x0a
#define PWM_RPMSG_VERSION			0x0001
#define PWM_RPMSG_TYPE_REQUEST			0x00
#define PWM_RPMSG_TYPE_RESPONSE			0x01
#define PWM_RPMSG_COMMAND_GET			0x00
#define PWM_RPMSG_COMMAND_SET			0x01

struct pwm_rpmsg_msg {
	struct imx_rpmsg_head header;

	/* Payload Start*/
	u8 request_id;
	u8 ret_val;
	u8 chip_id;
	u8 channel_id;
	u64 period;
	u64 duty_cycle;
	u8 polarity;
	u8 enabled;
} __packed __aligned(1);

struct pwm_rpmsg_info {
	struct rpmsg_device *rpdev;
	struct device *dev;
	struct pwm_rpmsg_msg *msg;
	struct completion cmd_complete;
	struct mutex lock;

	u8 chip_id;
	u8 channel_id;
	u8 request_id;
};

static struct pwm_rpmsg_info pwm_rpmsg;

struct imx_rpmsg_pwm_data {
	struct pwm_chip chip;
	u8 chip_id;
};

static inline struct imx_rpmsg_pwm_data *
to_imx_rpmsg_pwm_chip(struct pwm_chip *chip)
{
	return container_of(chip, struct imx_rpmsg_pwm_data, chip);
}

static int pwm_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len,
			   void *priv, u32 src)
{
	struct pwm_rpmsg_msg *msg = (struct pwm_rpmsg_msg *)data;

	if (msg->header.type != PWM_RPMSG_TYPE_RESPONSE)
		return -EINVAL;

	if (msg->request_id != pwm_rpmsg.request_id) {
		dev_err(&rpdev->dev,
		"expected request_id:%d, received request_id:%d, drop this recv\n",
		pwm_rpmsg.request_id, msg->request_id);

		/*
		 * This response does not match the request id.
		 * Drop it and wait for the right response.
		 */
		return 0;
	}

	if (msg->chip_id != pwm_rpmsg.chip_id ||
	    msg->channel_id != pwm_rpmsg.channel_id) {
		dev_err(&rpdev->dev,
		"expected chip_id:%d, channel_id:%2x, received chip_id:%d, channel_id:%2x\n",
		pwm_rpmsg.chip_id, pwm_rpmsg.channel_id,
		msg->chip_id, msg->channel_id);

		/*
		 * The chip_id or channel_id of this response does not match the
		 * request, but the request_id match. So return error.
		 */
		return -EINVAL;
	}

	/* Receive Success */
	pwm_rpmsg.msg = msg;

	complete(&pwm_rpmsg.cmd_complete);

	return 0;
}

static int rpmsg_xfer(struct pwm_rpmsg_msg *rmsg, struct pwm_rpmsg_info *info)
{
	int ret = 0;

	ret = rpmsg_send(info->rpdev->ept, (void *)rmsg,
						sizeof(struct pwm_rpmsg_msg));
	if (ret < 0) {
		dev_err(&info->rpdev->dev, "rpmsg_send failed: %d\n", ret);
		return ret;
	}

	ret = wait_for_completion_timeout(&info->cmd_complete,
					msecs_to_jiffies(PWM_RPMSG_TIMEOUT_MS));
	if (!ret) {
		dev_err(&info->rpdev->dev,
		"%s failed: timeout, target chip_id=%-2d, channel_id=0x%02X, %s state\n",
				__func__, rmsg->chip_id, rmsg->channel_id,
				(rmsg->header.cmd == 1) ? "SET" : "GET");
		return -ETIMEDOUT;
	}

	if (info->msg->ret_val) {
		dev_dbg(&info->rpdev->dev,
			"%s failed: %d\n", __func__, info->msg->ret_val);
		return -(info->msg->ret_val);
	}

	return 0;
}

static int pwm_rpsmg_get(struct pwm_rpmsg_info *info, struct pwm_state *state)
{
	int ret;
	struct pwm_rpmsg_msg rmsg;

	if (!info || !info->rpdev)
		return -EINVAL;

	memset(&rmsg, 0, sizeof(struct pwm_rpmsg_msg));
	rmsg.header.cate = PWM_RPMSG_CATEGORY;
	rmsg.header.major = PWM_RPMSG_VERSION;
	rmsg.header.minor = PWM_RPMSG_VERSION >> 8;
	rmsg.header.type = PWM_RPMSG_TYPE_REQUEST;
	rmsg.header.cmd = PWM_RPMSG_COMMAND_GET;
	rmsg.request_id = info->request_id;
	rmsg.chip_id = info->chip_id;
	rmsg.channel_id = info->channel_id;
	rmsg.period = state->period;
	rmsg.duty_cycle = state->duty_cycle;
	rmsg.polarity = state->polarity;
	rmsg.enabled = state->enabled;

	reinit_completion(&info->cmd_complete);

	ret = rpmsg_xfer(&rmsg, info);
	if (ret)
		return ret;

	state->period = info->msg->period;
	state->duty_cycle = info->msg->duty_cycle;
	state->polarity = info->msg->polarity;
	state->enabled = info->msg->enabled;

	return ret;
}

static int pwm_rpsmg_set(struct pwm_rpmsg_info *info, const struct pwm_state *state)
{
	int ret;
	struct pwm_rpmsg_msg rmsg;

	if (!info || !info->rpdev)
		return -EINVAL;

	memset(&rmsg, 0, sizeof(struct pwm_rpmsg_msg));
	rmsg.header.cate = PWM_RPMSG_CATEGORY;
	rmsg.header.major = PWM_RPMSG_VERSION;
	rmsg.header.minor = PWM_RPMSG_VERSION >> 8;
	rmsg.header.type = PWM_RPMSG_TYPE_REQUEST;
	rmsg.header.cmd = PWM_RPMSG_COMMAND_SET;
	rmsg.request_id = info->request_id;
	rmsg.chip_id = info->chip_id;
	rmsg.channel_id = info->channel_id;
	rmsg.period = state->period;
	rmsg.duty_cycle = state->duty_cycle;
	rmsg.polarity = state->polarity;
	rmsg.enabled = state->enabled;

	reinit_completion(&info->cmd_complete);

	ret = rpmsg_xfer(&rmsg, info);

	return ret;
}

static int pwm_rpmsg_probe(struct rpmsg_device *rpdev)
{
	int ret = 0;

	if (!rpdev) {
		dev_info(&rpdev->dev, "%s failed, rpdev=NULL\n", __func__);
		return -EINVAL;
	}

	pwm_rpmsg.rpdev = rpdev;

	mutex_init(&pwm_rpmsg.lock);
	init_completion(&pwm_rpmsg.cmd_complete);

	dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
						rpdev->src, rpdev->dst);

	return ret;
}

static void pwm_rpmsg_remove(struct rpmsg_device *rpdev)
{
	pwm_rpmsg.rpdev = NULL;
	dev_info(&rpdev->dev, "pwm rpmsg driver is removed\n");
}

static struct rpmsg_device_id pwm_rpmsg_id_table[] = {
	{ .name	= "rpmsg-pwm-channel" },
	{ },
};

static struct rpmsg_driver pwm_rpmsg_driver = {
	.drv.name	= "pwm-rpmsg",
	.drv.owner	= THIS_MODULE,
	.id_table	= pwm_rpmsg_id_table,
	.probe		= pwm_rpmsg_probe,
	.remove		= pwm_rpmsg_remove,
	.callback	= pwm_rpmsg_cb,
};

static void pwm_rpchip_get_state(struct pwm_chip *chip,
				  struct pwm_device *pwm,
				  struct pwm_state *state)
{
	struct imx_rpmsg_pwm_data *rdata = to_imx_rpmsg_pwm_chip(chip);
	int ret;

	mutex_lock(&pwm_rpmsg.lock);

	if (pwm_rpmsg.request_id >= 0xFF)
		pwm_rpmsg.request_id = 0;
	pwm_rpmsg.request_id++;

	pwm_rpmsg.chip_id = rdata->chip_id;
	pwm_rpmsg.channel_id = pwm->hwpwm;
	ret = pwm_rpsmg_get(&pwm_rpmsg, state);

	mutex_unlock(&pwm_rpmsg.lock);
}

static int pwm_rpchip_apply(struct pwm_chip *chip,
			     struct pwm_device *pwm,
			     const struct pwm_state *state)
{
	struct imx_rpmsg_pwm_data *rdata = to_imx_rpmsg_pwm_chip(chip);
	int ret;

	mutex_lock(&pwm_rpmsg.lock);

	if (pwm_rpmsg.request_id >= 0xFF)
		pwm_rpmsg.request_id = 0;
	pwm_rpmsg.request_id++;

	pwm_rpmsg.chip_id = rdata->chip_id;
	pwm_rpmsg.channel_id = pwm->hwpwm;
	ret = pwm_rpsmg_set(&pwm_rpmsg, state);

	mutex_unlock(&pwm_rpmsg.lock);

	return ret;
}

static const struct pwm_ops imx_tpm_pwm_ops = {
	.get_state = pwm_rpchip_get_state,
	.apply = pwm_rpchip_apply,
	.owner = THIS_MODULE,
};

static int pwm_rpchip_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct imx_rpmsg_pwm_data *rdata;
	struct pwm_chip *chip;
	int ret;

	if (!pwm_rpmsg.rpdev)
		return -EPROBE_DEFER;

	rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata), GFP_KERNEL);
	if (!rdata)
		return -ENOMEM;

	chip = &rdata->chip;
	/* setup pwm chip description */
	chip->dev = &pdev->dev;
	chip->ops = &imx_tpm_pwm_ops;
	chip->base = -1;
	chip->of_xlate = of_pwm_xlate_with_flags;
	chip->of_pwm_n_cells = 3;
	ret = of_property_read_u32(np, "fsl,pwm-channel-number", &chip->npwm);
	if (ret < 0) {
		dev_err(dev, "failed to read pwm channel number from dts: %d\n",
			     ret);
		return -EINVAL;
	}
	rdata->chip_id = of_alias_get_id(np, "pwm");
	if (rdata->chip_id < 0) {
		dev_err(dev, "failed to get pwm alias number: %d\n",
			     rdata->chip_id);
		return -EINVAL;
	}

	platform_set_drvdata(pdev, rdata);

	ret = devm_pwmchip_add(&pdev->dev, &rdata->chip);
	if (ret) {
		dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
		return ret;
	}

	dev_info(dev, "add PWM chip %d successfully\n", rdata->chip_id);

	return ret;
}

static const struct of_device_id imx_rpmsg_pwm_dt_ids[] = {
	{ .compatible = "fsl,pwm-rpchip", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_rpmsg_pwm_dt_ids);

static struct platform_driver imx_rpmsg_pwm_driver = {
	.driver = {
		.name	= "imx_rpmsg_pwm",
		.of_match_table = imx_rpmsg_pwm_dt_ids,
	},
	.probe		= pwm_rpchip_probe,
};

static int __init imx_rpmsg_pwm_driver_init(void)
{
	int ret = 0;

	ret = register_rpmsg_driver(&pwm_rpmsg_driver);
	if (ret < 0)
		return ret;

	return platform_driver_register(&(imx_rpmsg_pwm_driver));
}
device_initcall(imx_rpmsg_pwm_driver_init);

MODULE_AUTHOR("Clark Wang<xiaoning.wang@nxp.com>");
MODULE_DESCRIPTION("Driver for pwm over rpmsg");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:pwm-rpchip");