1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/idr.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/platform_device.h>
21#include <linux/spmi.h>
22#include <linux/pm_runtime.h>
23
24#include <dt-bindings/spmi/spmi.h>
25
26static DEFINE_IDA(ctrl_ida);
27
28static void spmi_dev_release(struct device *dev)
29{
30 struct spmi_device *sdev = to_spmi_device(dev);
31 kfree(sdev);
32}
33
34static const struct device_type spmi_dev_type = {
35 .release = spmi_dev_release,
36};
37
38static void spmi_ctrl_release(struct device *dev)
39{
40 struct spmi_controller *ctrl = to_spmi_controller(dev);
41 ida_simple_remove(&ctrl_ida, ctrl->nr);
42 kfree(ctrl);
43}
44
45static const struct device_type spmi_ctrl_type = {
46 .release = spmi_ctrl_release,
47};
48
49static int spmi_device_match(struct device *dev, struct device_driver *drv)
50{
51 if (of_driver_match_device(dev, drv))
52 return 1;
53
54 if (drv->name)
55 return strncmp(dev_name(dev), drv->name,
56 SPMI_NAME_SIZE) == 0;
57
58 return 0;
59}
60
61
62
63
64
65int spmi_device_add(struct spmi_device *sdev)
66{
67 struct spmi_controller *ctrl = sdev->ctrl;
68 int err;
69
70 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
71
72 err = device_add(&sdev->dev);
73 if (err < 0) {
74 dev_err(&sdev->dev, "Can't add %s, status %d\n",
75 dev_name(&sdev->dev), err);
76 goto err_device_add;
77 }
78
79 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
80
81err_device_add:
82 return err;
83}
84EXPORT_SYMBOL_GPL(spmi_device_add);
85
86
87
88
89
90void spmi_device_remove(struct spmi_device *sdev)
91{
92 device_unregister(&sdev->dev);
93}
94EXPORT_SYMBOL_GPL(spmi_device_remove);
95
96static inline int
97spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
98{
99 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
100 return -EINVAL;
101
102 return ctrl->cmd(ctrl, opcode, sid);
103}
104
105static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
106 u8 sid, u16 addr, u8 *buf, size_t len)
107{
108 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
109 return -EINVAL;
110
111 return ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
112}
113
114static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
115 u8 sid, u16 addr, const u8 *buf, size_t len)
116{
117 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
118 return -EINVAL;
119
120 return ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
121}
122
123
124
125
126
127
128
129
130
131int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
132{
133
134 if (addr > 0x1F)
135 return -EINVAL;
136
137 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
138 buf, 1);
139}
140EXPORT_SYMBOL_GPL(spmi_register_read);
141
142
143
144
145
146
147
148
149
150
151
152int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
153 size_t len)
154{
155
156 if (len == 0 || len > 16)
157 return -EINVAL;
158
159 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
160 buf, len);
161}
162EXPORT_SYMBOL_GPL(spmi_ext_register_read);
163
164
165
166
167
168
169
170
171
172
173
174int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
175 size_t len)
176{
177
178 if (len == 0 || len > 8)
179 return -EINVAL;
180
181 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
182 buf, len);
183}
184EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
185
186
187
188
189
190
191
192
193
194int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
195{
196
197 if (addr > 0x1F)
198 return -EINVAL;
199
200 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
201 &data, 1);
202}
203EXPORT_SYMBOL_GPL(spmi_register_write);
204
205
206
207
208
209
210
211
212int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
213{
214 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
215 &data, 1);
216}
217EXPORT_SYMBOL_GPL(spmi_register_zero_write);
218
219
220
221
222
223
224
225
226
227
228
229int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
230 size_t len)
231{
232
233 if (len == 0 || len > 16)
234 return -EINVAL;
235
236 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
237 buf, len);
238}
239EXPORT_SYMBOL_GPL(spmi_ext_register_write);
240
241
242
243
244
245
246
247
248
249
250
251int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
252 size_t len)
253{
254
255 if (len == 0 || len > 8)
256 return -EINVAL;
257
258 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
259 addr, buf, len);
260}
261EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
262
263
264
265
266
267
268
269
270
271int spmi_command_reset(struct spmi_device *sdev)
272{
273 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
274}
275EXPORT_SYMBOL_GPL(spmi_command_reset);
276
277
278
279
280
281
282
283int spmi_command_sleep(struct spmi_device *sdev)
284{
285 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
286}
287EXPORT_SYMBOL_GPL(spmi_command_sleep);
288
289
290
291
292
293
294
295
296int spmi_command_wakeup(struct spmi_device *sdev)
297{
298 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
299}
300EXPORT_SYMBOL_GPL(spmi_command_wakeup);
301
302
303
304
305
306
307
308int spmi_command_shutdown(struct spmi_device *sdev)
309{
310 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
311}
312EXPORT_SYMBOL_GPL(spmi_command_shutdown);
313
314static int spmi_drv_probe(struct device *dev)
315{
316 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
317 struct spmi_device *sdev = to_spmi_device(dev);
318 int err;
319
320 pm_runtime_get_noresume(dev);
321 pm_runtime_set_active(dev);
322 pm_runtime_enable(dev);
323
324 err = sdrv->probe(sdev);
325 if (err)
326 goto fail_probe;
327
328 return 0;
329
330fail_probe:
331 pm_runtime_disable(dev);
332 pm_runtime_set_suspended(dev);
333 pm_runtime_put_noidle(dev);
334 return err;
335}
336
337static int spmi_drv_remove(struct device *dev)
338{
339 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
340
341 pm_runtime_get_sync(dev);
342 sdrv->remove(to_spmi_device(dev));
343 pm_runtime_put_noidle(dev);
344
345 pm_runtime_disable(dev);
346 pm_runtime_set_suspended(dev);
347 pm_runtime_put_noidle(dev);
348 return 0;
349}
350
351static struct bus_type spmi_bus_type = {
352 .name = "spmi",
353 .match = spmi_device_match,
354 .probe = spmi_drv_probe,
355 .remove = spmi_drv_remove,
356};
357
358
359
360
361
362
363
364
365struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
366{
367 struct spmi_device *sdev;
368
369 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
370 if (!sdev)
371 return NULL;
372
373 sdev->ctrl = ctrl;
374 device_initialize(&sdev->dev);
375 sdev->dev.parent = &ctrl->dev;
376 sdev->dev.bus = &spmi_bus_type;
377 sdev->dev.type = &spmi_dev_type;
378 return sdev;
379}
380EXPORT_SYMBOL_GPL(spmi_device_alloc);
381
382
383
384
385
386
387
388
389
390
391
392struct spmi_controller *spmi_controller_alloc(struct device *parent,
393 size_t size)
394{
395 struct spmi_controller *ctrl;
396 int id;
397
398 if (WARN_ON(!parent))
399 return NULL;
400
401 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
402 if (!ctrl)
403 return NULL;
404
405 device_initialize(&ctrl->dev);
406 ctrl->dev.type = &spmi_ctrl_type;
407 ctrl->dev.bus = &spmi_bus_type;
408 ctrl->dev.parent = parent;
409 ctrl->dev.of_node = parent->of_node;
410 spmi_controller_set_drvdata(ctrl, &ctrl[1]);
411
412 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
413 if (id < 0) {
414 dev_err(parent,
415 "unable to allocate SPMI controller identifier.\n");
416 spmi_controller_put(ctrl);
417 return NULL;
418 }
419
420 ctrl->nr = id;
421 dev_set_name(&ctrl->dev, "spmi-%d", id);
422
423 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
424 return ctrl;
425}
426EXPORT_SYMBOL_GPL(spmi_controller_alloc);
427
428static void of_spmi_register_devices(struct spmi_controller *ctrl)
429{
430 struct device_node *node;
431 int err;
432
433 if (!ctrl->dev.of_node)
434 return;
435
436 for_each_available_child_of_node(ctrl->dev.of_node, node) {
437 struct spmi_device *sdev;
438 u32 reg[2];
439
440 dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
441
442 err = of_property_read_u32_array(node, "reg", reg, 2);
443 if (err) {
444 dev_err(&ctrl->dev,
445 "node %s err (%d) does not have 'reg' property\n",
446 node->full_name, err);
447 continue;
448 }
449
450 if (reg[1] != SPMI_USID) {
451 dev_err(&ctrl->dev,
452 "node %s contains unsupported 'reg' entry\n",
453 node->full_name);
454 continue;
455 }
456
457 if (reg[0] >= SPMI_MAX_SLAVE_ID) {
458 dev_err(&ctrl->dev,
459 "invalid usid on node %s\n",
460 node->full_name);
461 continue;
462 }
463
464 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
465
466 sdev = spmi_device_alloc(ctrl);
467 if (!sdev)
468 continue;
469
470 sdev->dev.of_node = node;
471 sdev->usid = (u8) reg[0];
472
473 err = spmi_device_add(sdev);
474 if (err) {
475 dev_err(&sdev->dev,
476 "failure adding device. status %d\n", err);
477 spmi_device_put(sdev);
478 }
479 }
480}
481
482
483
484
485
486
487
488
489int spmi_controller_add(struct spmi_controller *ctrl)
490{
491 int ret;
492
493
494 if (WARN_ON(!spmi_bus_type.p))
495 return -EAGAIN;
496
497 ret = device_add(&ctrl->dev);
498 if (ret)
499 return ret;
500
501 if (IS_ENABLED(CONFIG_OF))
502 of_spmi_register_devices(ctrl);
503
504 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
505 ctrl->nr, &ctrl->dev);
506
507 return 0;
508};
509EXPORT_SYMBOL_GPL(spmi_controller_add);
510
511
512static int spmi_ctrl_remove_device(struct device *dev, void *data)
513{
514 struct spmi_device *spmidev = to_spmi_device(dev);
515 if (dev->type == &spmi_dev_type)
516 spmi_device_remove(spmidev);
517 return 0;
518}
519
520
521
522
523
524
525
526
527void spmi_controller_remove(struct spmi_controller *ctrl)
528{
529 int dummy;
530
531 if (!ctrl)
532 return;
533
534 dummy = device_for_each_child(&ctrl->dev, NULL,
535 spmi_ctrl_remove_device);
536 device_del(&ctrl->dev);
537}
538EXPORT_SYMBOL_GPL(spmi_controller_remove);
539
540
541
542
543
544
545
546
547int spmi_driver_register(struct spmi_driver *sdrv)
548{
549 sdrv->driver.bus = &spmi_bus_type;
550 return driver_register(&sdrv->driver);
551}
552EXPORT_SYMBOL_GPL(spmi_driver_register);
553
554static void __exit spmi_exit(void)
555{
556 bus_unregister(&spmi_bus_type);
557}
558module_exit(spmi_exit);
559
560static int __init spmi_init(void)
561{
562 return bus_register(&spmi_bus_type);
563}
564postcore_initcall(spmi_init);
565
566MODULE_LICENSE("GPL v2");
567MODULE_DESCRIPTION("SPMI module");
568MODULE_ALIAS("platform:spmi");
569