Blame view

drivers/regulator/pf1550-regulator-rpmsg.c 11.3 KB
1de219078   Robin Gong   MLK-13577-3 regul...
1
2
3
4
  /*
   * pf1550.c - regulator driver for the PF1550
   *
   * Copyright (C) 2016 Freescale Semiconductor, Inc.
679321a1c   Robin Gong   MLK-13733-3 regul...
5
   * Copyright (C) 2017 NXP.
1de219078   Robin Gong   MLK-13577-3 regul...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   * Robin Gong <yibin.gong@freescale.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   */
  
  #include <linux/err.h>
  #include <linux/slab.h>
679321a1c   Robin Gong   MLK-13733-3 regul...
22
  #include <linux/imx_rpmsg.h>
1de219078   Robin Gong   MLK-13577-3 regul...
23
24
25
26
  #include <linux/interrupt.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
8504c576a   Robin Gong   MLK-13636-2 regul...
27
  #include <linux/pm_qos.h>
1de219078   Robin Gong   MLK-13577-3 regul...
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
  #include <linux/regulator/driver.h>
  #include <linux/regulator/machine.h>
  #include <linux/regulator/of_regulator.h>
  #include <linux/regulator/machine.h>
  #include <linux/rpmsg.h>
  #include <linux/uaccess.h>
  #include <linux/virtio.h>
  
  #define PF1550_DBGFS
  #define PF1550_MAX_REGULATOR 7
  #define RPMSG_TIMEOUT 1000
  
  enum pf1550_regs {
  	PF1550_SW1,
  	PF1550_SW2,
  	PF1550_SW3,
  	PF1550_VREFDDR,
  	PF1550_LDO1,
  	PF1550_LDO2,
  	PF1550_LDO3,
  };
  
  enum pf1550_rpmsg_cmd {
  	PF1550_ENABLE,
  	PF1550_DISABLE,
  	PF1550_IS_ENABLED,
  	PF1550_SET_VOL,
  	PF1550_GET_VOL,
  	PF1550_GET_REG,
  	PF1550_SET_REG,
  };
  
  enum pf1550_resp {
1de219078   Robin Gong   MLK-13577-3 regul...
61
  	PF1550_SUCCESS,
679321a1c   Robin Gong   MLK-13733-3 regul...
62
63
64
65
66
  	PF1550_FAILED,
  	PF1550_UNSURPPORT,
  };
  
  enum pf1550_status {
1de219078   Robin Gong   MLK-13577-3 regul...
67
  	PF1550_DISABLED,
679321a1c   Robin Gong   MLK-13733-3 regul...
68
  	PF1550_ENABLED,
1de219078   Robin Gong   MLK-13577-3 regul...
69
70
71
72
73
74
75
  };
  
  struct pf1550_regulator_info {
  	struct rpmsg_device *rpdev;
  	struct device *dev;
  	struct pf1550_regulator_rpmsg *msg;
  	struct completion cmd_complete;
8504c576a   Robin Gong   MLK-13636-2 regul...
76
  	struct pm_qos_request pm_qos_req;
2542a23d9   Robin Gong   MLK-14241-1 regul...
77
  	struct mutex lock;
1de219078   Robin Gong   MLK-13577-3 regul...
78
79
80
81
82
83
  	struct regulator_desc *regulators;
  };
  
  static struct pf1550_regulator_info pf1550_info;
  
  struct pf1550_regulator_rpmsg {
679321a1c   Robin Gong   MLK-13733-3 regul...
84
85
86
  	/* common head */
  	struct imx_rpmsg_head header;
  	/* pmic structure */
1de219078   Robin Gong   MLK-13577-3 regul...
87
  	union {
679321a1c   Robin Gong   MLK-13733-3 regul...
88
89
  		u8 regulator;
  		u8 reg;
1de219078   Robin Gong   MLK-13577-3 regul...
90
  	};
679321a1c   Robin Gong   MLK-13733-3 regul...
91
92
  	u8 response;
  	u8 status;
1de219078   Robin Gong   MLK-13577-3 regul...
93
94
95
96
  	union {
  		u32 voltage; /* uV */
  		u32 val;
  	};
679321a1c   Robin Gong   MLK-13733-3 regul...
97
  } __attribute__ ((packed));
1de219078   Robin Gong   MLK-13577-3 regul...
98
99
100
101
102
  
  static int pf1550_send_message(struct pf1550_regulator_rpmsg *msg,
  			       struct pf1550_regulator_info *info)
  {
  	int err;
1de219078   Robin Gong   MLK-13577-3 regul...
103
104
105
106
107
108
  	if (!info->rpdev) {
  		dev_dbg(info->dev,
  			 "rpmsg channel not ready, m4 image ready?
  ");
  		return -EINVAL;
  	}
2542a23d9   Robin Gong   MLK-14241-1 regul...
109
  	mutex_lock(&info->lock);
8504c576a   Robin Gong   MLK-13636-2 regul...
110
  	pm_qos_add_request(&info->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0);
2542a23d9   Robin Gong   MLK-14241-1 regul...
111
112
113
114
  	msg->header.cate = IMX_RPMSG_PMIC;
  	msg->header.major = IMX_RMPSG_MAJOR;
  	msg->header.minor = IMX_RMPSG_MINOR;
  	msg->header.type = 0;
9012680f6   Robin Gong   MLK-13720-4: regu...
115
116
  	/* wait response from rpmsg */
  	reinit_completion(&info->cmd_complete);
1de219078   Robin Gong   MLK-13577-3 regul...
117
118
119
120
121
  	err = rpmsg_send(info->rpdev->ept, (void *)msg,
  			    sizeof(struct pf1550_regulator_rpmsg));
  	if (err) {
  		dev_err(&info->rpdev->dev, "rpmsg_send failed: %d
  ", err);
2542a23d9   Robin Gong   MLK-14241-1 regul...
122
  		goto err_out;
1de219078   Robin Gong   MLK-13577-3 regul...
123
  	}
1de219078   Robin Gong   MLK-13577-3 regul...
124
125
126
127
128
  	err = wait_for_completion_timeout(&info->cmd_complete,
  					  msecs_to_jiffies(RPMSG_TIMEOUT));
  	if (!err) {
  		dev_err(&info->rpdev->dev, "rpmsg_send timeout!
  ");
2542a23d9   Robin Gong   MLK-14241-1 regul...
129
130
  		err = -ETIMEDOUT;
  		goto err_out;
1de219078   Robin Gong   MLK-13577-3 regul...
131
  	}
2542a23d9   Robin Gong   MLK-14241-1 regul...
132
133
134
135
136
  	err = 0;
  
  err_out:
  	pm_qos_remove_request(&info->pm_qos_req);
  	mutex_unlock(&info->lock);
1de219078   Robin Gong   MLK-13577-3 regul...
137
138
  	dev_dbg(&info->rpdev->dev, "cmd:%d, reg:%d, resp:%d.
  ",
679321a1c   Robin Gong   MLK-13733-3 regul...
139
  		  msg->header.cmd, msg->regulator, msg->response);
1de219078   Robin Gong   MLK-13577-3 regul...
140

2542a23d9   Robin Gong   MLK-14241-1 regul...
141
  	return err;
1de219078   Robin Gong   MLK-13577-3 regul...
142
143
144
145
146
147
  }
  
  static int pf1550_enable(struct regulator_dev *reg)
  {
  	struct pf1550_regulator_info *info = rdev_get_drvdata(reg);
  	struct pf1550_regulator_rpmsg msg;
679321a1c   Robin Gong   MLK-13733-3 regul...
148
  	msg.header.cmd = PF1550_ENABLE;
1de219078   Robin Gong   MLK-13577-3 regul...
149
150
151
152
153
154
155
156
157
  	msg.regulator = reg->desc->id;
  
  	return pf1550_send_message(&msg, info);
  }
  
  static int pf1550_disable(struct regulator_dev *reg)
  {
  	struct pf1550_regulator_info *info = rdev_get_drvdata(reg);
  	struct pf1550_regulator_rpmsg msg;
679321a1c   Robin Gong   MLK-13733-3 regul...
158
  	msg.header.cmd = PF1550_DISABLE;
1de219078   Robin Gong   MLK-13577-3 regul...
159
160
161
162
163
164
165
166
167
168
  	msg.regulator = reg->desc->id;
  
  	return pf1550_send_message(&msg, info);
  }
  
  static int pf1550_is_enabled(struct regulator_dev *reg)
  {
  	struct pf1550_regulator_info *info = rdev_get_drvdata(reg);
  	struct pf1550_regulator_rpmsg msg;
  	int err;
679321a1c   Robin Gong   MLK-13733-3 regul...
169
  	msg.header.cmd = PF1550_IS_ENABLED;
1de219078   Robin Gong   MLK-13577-3 regul...
170
171
172
173
174
175
  	msg.regulator = reg->desc->id;
  
  	err = pf1550_send_message(&msg, info);
  	if (err)
  		return err;
  	/* Here SUCCESS means ENABLED */
679321a1c   Robin Gong   MLK-13733-3 regul...
176
  	if (info->msg->status == PF1550_ENABLED)
1de219078   Robin Gong   MLK-13577-3 regul...
177
178
179
180
181
182
183
184
185
186
187
  		return 1;
  	else
  		return 0;
  }
  
  static int pf1550_set_voltage(struct regulator_dev *reg,
  			      int minuV, int uV, unsigned *selector)
  {
  	struct pf1550_regulator_info *info = rdev_get_drvdata(reg);
  	struct pf1550_regulator_rpmsg msg;
  	int err;
679321a1c   Robin Gong   MLK-13733-3 regul...
188
  	msg.header.cmd = PF1550_SET_VOL;
1de219078   Robin Gong   MLK-13577-3 regul...
189
  	msg.regulator = reg->desc->id;
ffe2faa2f   Dong Aisheng   MLK-13615-1 regul...
190
  	msg.voltage = minuV;
1de219078   Robin Gong   MLK-13577-3 regul...
191
192
193
194
  
  	err = pf1550_send_message(&msg, info);
  	if (err)
  		return err;
679321a1c   Robin Gong   MLK-13733-3 regul...
195
  	if (info->msg->response == PF1550_UNSURPPORT) {
1de219078   Robin Gong   MLK-13577-3 regul...
196
197
198
199
200
201
202
203
204
205
206
207
208
  		dev_err(info->dev, "Voltages not allowed to set to %d!
  ", uV);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int pf1550_get_voltage(struct regulator_dev *reg)
  {
  	struct pf1550_regulator_info *info = rdev_get_drvdata(reg);
  	struct pf1550_regulator_rpmsg msg;
  	int err;
679321a1c   Robin Gong   MLK-13733-3 regul...
209
  	msg.header.cmd = PF1550_GET_VOL;
1de219078   Robin Gong   MLK-13577-3 regul...
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
  	msg.regulator = reg->desc->id;
  	msg.voltage = 0;
  
  	err = pf1550_send_message(&msg, info);
  	if (err)
  		return err;
  
  	return info->msg->voltage;
  }
  
  /* return the fix voltage */
  static int pf1550_get_fix_voltage(struct regulator_dev *dev)
  {
  	return dev->desc->fixed_uV;
  }
  
  static struct regulator_ops pf1550_sw_ops = {
  	.set_voltage = pf1550_set_voltage,
  	.get_voltage = pf1550_get_voltage,
  };
  
  static struct regulator_ops pf1550_ldo_ops = {
  	.enable = pf1550_enable,
  	.disable = pf1550_disable,
  	.is_enabled = pf1550_is_enabled,
  	.set_voltage = pf1550_set_voltage,
  	.get_voltage = pf1550_get_voltage,
  };
  
  static struct regulator_ops pf1550_fixed_ops = {
  	.enable = pf1550_enable,
  	.disable = pf1550_disable,
  	.get_voltage = pf1550_get_fix_voltage,
  	.is_enabled = pf1550_is_enabled,
  };
  
  static struct regulator_desc pf1550_regulators[PF1550_MAX_REGULATOR] = {
  {
  	.name = "SW1",
  	.of_match = of_match_ptr("SW1"),
  	.id = PF1550_SW1,
  	.ops = &pf1550_sw_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "SW2",
  	.of_match = of_match_ptr("SW2"),
  	.id = PF1550_SW2,
  	.ops = &pf1550_sw_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "SW3",
  	.of_match = of_match_ptr("SW3"),
  	.id = PF1550_SW3,
  	.ops = &pf1550_sw_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "VREFDDR",
  	.of_match = of_match_ptr("VREFDDR"),
  	.id = PF1550_VREFDDR,
  	.ops = &pf1550_fixed_ops,
  	.type = REGULATOR_VOLTAGE,
  	.fixed_uV = 1200000,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "LDO1",
  	.of_match = of_match_ptr("LDO1"),
  	.id = PF1550_LDO1,
  	.ops = &pf1550_ldo_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "LDO2",
  	.of_match = of_match_ptr("LDO2"),
  	.id = PF1550_LDO2,
  	.ops = &pf1550_ldo_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  {
  	.name = "LDO3",
  	.of_match = of_match_ptr("LDO3"),
  	.id = PF1550_LDO3,
  	.ops = &pf1550_ldo_ops,
  	.type = REGULATOR_VOLTAGE,
  	.owner = THIS_MODULE,
  },
  };
  
  
  static int rpmsg_regulator_cb(struct rpmsg_device *rpdev, void *data, int len,
  			      void *priv, u32 src)
  {
  	struct pf1550_regulator_rpmsg *msg = (struct pf1550_regulator_rpmsg *)data;
  
  
  	dev_dbg(&rpdev->dev, "get from%d: cmd:%d, reg:%d, resp:%d.
  ",
679321a1c   Robin Gong   MLK-13733-3 regul...
315
  		  src, msg->header.cmd, msg->regulator, msg->response);
1de219078   Robin Gong   MLK-13577-3 regul...
316
317
318
319
320
321
322
323
324
325
326
327
328
  
  	pf1550_info.msg = msg;
  
  	complete(&pf1550_info.cmd_complete);
  
  	return 0;
  }
  
  static int rpmsg_regulator_probe(struct rpmsg_device *rpdev)
  {
  	pf1550_info.rpdev = rpdev;
  
  	init_completion(&pf1550_info.cmd_complete);
2542a23d9   Robin Gong   MLK-14241-1 regul...
329
  	mutex_init(&pf1550_info.lock);
1de219078   Robin Gong   MLK-13577-3 regul...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  
  	dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!
  ",
  			rpdev->src, rpdev->dst);
  	return 0;
  }
  
  static void rpmsg_regulator_remove(struct rpmsg_device *rpdev)
  {
  	dev_info(&rpdev->dev, "rpmsg regulator driver is removed
  ");
  }
  
  static struct rpmsg_device_id rpmsg_regulator_id_table[] = {
c2a260b86   Robin Gong   MLK-13720-3: regu...
344
  	{ .name	= "rpmsg-regulator-channel" },
1de219078   Robin Gong   MLK-13577-3 regul...
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
  	{ },
  };
  
  static struct rpmsg_driver rpmsg_regulator_driver = {
  	.drv.name	= "regulator_rpmsg",
  	.drv.owner	= THIS_MODULE,
  	.id_table	= rpmsg_regulator_id_table,
  	.probe		= rpmsg_regulator_probe,
  	.callback	= rpmsg_regulator_cb,
  	.remove		= rpmsg_regulator_remove,
  };
  
  #ifdef PF1550_DBGFS
  #define MAX_REGS 0xff
  
  /*
   * Alligned the below two functions as the same as regmap_map_read_file
   * and regmap_map_write_file in regmap-debugfs.c
   */
  static ssize_t pf1550_registers_show(struct device *dev,
  				struct device_attribute *attr, char *buf)
  {
  	int i;
  	struct platform_device *pdev = to_platform_device(dev);
  	struct pf1550_regulator_info *info = platform_get_drvdata(pdev);
  	struct pf1550_regulator_rpmsg msg;
  	int err;
  	size_t bufpos = 0, count = MAX_REGS * 7;
  
  	for (i = 0; i < MAX_REGS; i++) {
  		snprintf(buf + bufpos, count - bufpos, "%.*x: ", 2, i);
  		bufpos += 4;
679321a1c   Robin Gong   MLK-13733-3 regul...
377
  		msg.header.cmd = PF1550_GET_REG;
1de219078   Robin Gong   MLK-13577-3 regul...
378
  		msg.reg = i;
a7c656a8a   Robin Gong   MLK-14275: regula...
379
  		msg.val = 0;
1de219078   Robin Gong   MLK-13577-3 regul...
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
  
  		err = pf1550_send_message(&msg, info);
  		if (err)
  			return err;
  
  		if (info->msg->response != PF1550_SUCCESS) {
  			dev_err(info->dev, "Get register failed %x, resp=%x!
  ",
  				i, info->msg->response);
  			return -EINVAL;
  		}
  
  		snprintf(buf + bufpos, count - bufpos, "%.*x
  ", 2,
  			 info->msg->val);
  		bufpos += 2;
  
  		buf[bufpos++] = '
  ';
  	}
  
  	return bufpos;
  }
  
  static ssize_t pf1550_register_store(struct device *dev,
  				 struct device_attribute *attr,
  				 const char *buf, size_t size)
  {
  	struct platform_device *pdev = to_platform_device(dev);
  	struct pf1550_regulator_info *info = platform_get_drvdata(pdev);
  	struct pf1550_regulator_rpmsg msg;
  	char *start = (char *)buf;
  	unsigned long reg, value;
  	int err;
  
  	while (*start == ' ')
  		start++;
  	reg = simple_strtoul(start, &start, 16);
  
  	while (*start == ' ')
  		start++;
  	if (kstrtoul(start, 16, &value))
  		return -EINVAL;
679321a1c   Robin Gong   MLK-13733-3 regul...
423
  	msg.header.cmd = PF1550_SET_REG;
1de219078   Robin Gong   MLK-13577-3 regul...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  	msg.reg = reg;
  	msg.val = value;
  
  	err = pf1550_send_message(&msg, info);
  	if (err)
  		return err;
  
  	if (info->msg->response != PF1550_SUCCESS) {
  		dev_err(info->dev, "set register failed %lx!
  ", reg);
  		return -EINVAL;
  	}
  
  	return size;
  }
  
  static DEVICE_ATTR(regs, 0644, pf1550_registers_show, pf1550_register_store);
  #endif
  
  static int pf1550_regulator_probe(struct platform_device *pdev)
  {
  	struct device_node *np = pdev->dev.of_node;
  	int i;
  	struct regulator_config config = { };
  
  	if (!np)
  		return -ENODEV;
  
  	config.dev = &pdev->dev;
  	config.driver_data = &pf1550_info;
  	pf1550_info.dev = &pdev->dev;
  	pf1550_info.regulators = pf1550_regulators;
  
  	for (i = 0; i < ARRAY_SIZE(pf1550_regulators); i++) {
  		struct regulator_dev *rdev;
  		struct regulator_desc *desc;
  
  		desc = &pf1550_info.regulators[i];
  		rdev = devm_regulator_register(&pdev->dev, desc, &config);
  		if (IS_ERR(rdev)) {
  			dev_err(&pdev->dev,
  				"Failed to initialize regulator-%d
  ", i);
  			return PTR_ERR(rdev);
  		}
  	}
  
  #ifdef PF1550_DBGFS
  	i = sysfs_create_file(&config.dev->kobj, &dev_attr_regs.attr);
  	if (i) {
  		dev_err(&pdev->dev, "Failed to create pf1550 debug sysfs.
  ");
  		return i;
  	}
  #endif
2542a23d9   Robin Gong   MLK-14241-1 regul...
479

1de219078   Robin Gong   MLK-13577-3 regul...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  	platform_set_drvdata(pdev, &pf1550_info);
  
  	return 0;
  }
  
  static const struct of_device_id pf1550_regulator_id[] = {
  	{"fsl,pf1550-rpmsg",},
  	{},
  };
  
  MODULE_DEVICE_TABLE(of, pf1550_regulator_id);
  
  static struct platform_driver pf1550_regulator_driver = {
  	.driver = {
  		   .name = "pf1550-rpmsg",
  		   .owner = THIS_MODULE,
  		   .of_match_table = pf1550_regulator_id,
  		   },
  	.probe = pf1550_regulator_probe,
  };
  
  static int __init pf1550_rpmsg_init(void)
  {
  	return register_rpmsg_driver(&rpmsg_regulator_driver);
  }
  
  module_platform_driver(pf1550_regulator_driver);
  module_init(pf1550_rpmsg_init);
  
  MODULE_DESCRIPTION("Freescale PF1550 regulator rpmsg driver");
  MODULE_AUTHOR("Robin Gong <yibin.gong@freescale.com>");
  MODULE_LICENSE("GPL");