Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
 
 
 
 
 
 
 
 
 
  4 */
  5#include <linux/kernel.h>
  6#include <linux/errno.h>
  7#include <linux/idr.h>
  8#include <linux/slab.h>
  9#include <linux/module.h>
 10#include <linux/of.h>
 11#include <linux/of_device.h>
 12#include <linux/platform_device.h>
 13#include <linux/spmi.h>
 14#include <linux/pm_runtime.h>
 15
 16#include <dt-bindings/spmi/spmi.h>
 17#define CREATE_TRACE_POINTS
 18#include <trace/events/spmi.h>
 19
 20static bool is_registered;
 21static DEFINE_IDA(ctrl_ida);
 22
 23static void spmi_dev_release(struct device *dev)
 24{
 25	struct spmi_device *sdev = to_spmi_device(dev);
 26
 27	kfree(sdev);
 28}
 29
 30static const struct device_type spmi_dev_type = {
 31	.release	= spmi_dev_release,
 32};
 33
 34static void spmi_ctrl_release(struct device *dev)
 35{
 36	struct spmi_controller *ctrl = to_spmi_controller(dev);
 37
 38	ida_free(&ctrl_ida, ctrl->nr);
 39	kfree(ctrl);
 40}
 41
 42static const struct device_type spmi_ctrl_type = {
 43	.release	= spmi_ctrl_release,
 44};
 45
 46static int spmi_device_match(struct device *dev, struct device_driver *drv)
 47{
 48	if (of_driver_match_device(dev, drv))
 49		return 1;
 50
 51	if (drv->name)
 52		return strncmp(dev_name(dev), drv->name,
 53			       SPMI_NAME_SIZE) == 0;
 54
 55	return 0;
 56}
 57
 58/**
 59 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
 60 * @sdev:	spmi_device to be added
 61 */
 62int spmi_device_add(struct spmi_device *sdev)
 63{
 64	struct spmi_controller *ctrl = sdev->ctrl;
 65	int err;
 66
 67	dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
 68
 69	err = device_add(&sdev->dev);
 70	if (err < 0) {
 71		dev_err(&sdev->dev, "Can't add %s, status %d\n",
 72			dev_name(&sdev->dev), err);
 73		goto err_device_add;
 74	}
 75
 76	dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
 77
 78err_device_add:
 79	return err;
 80}
 81EXPORT_SYMBOL_GPL(spmi_device_add);
 82
 83/**
 84 * spmi_device_remove(): remove an SPMI device
 85 * @sdev:	spmi_device to be removed
 86 */
 87void spmi_device_remove(struct spmi_device *sdev)
 88{
 89	device_unregister(&sdev->dev);
 90}
 91EXPORT_SYMBOL_GPL(spmi_device_remove);
 92
 93static inline int
 94spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
 95{
 96	int ret;
 97
 98	if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
 99		return -EINVAL;
100
101	ret = ctrl->cmd(ctrl, opcode, sid);
102	trace_spmi_cmd(opcode, sid, ret);
103	return ret;
104}
105
106static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
107				u8 sid, u16 addr, u8 *buf, size_t len)
108{
109	int ret;
110
111	if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
112		return -EINVAL;
113
114	trace_spmi_read_begin(opcode, sid, addr);
115	ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
116	trace_spmi_read_end(opcode, sid, addr, ret, len, buf);
117	return ret;
118}
119
120static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
121				 u8 sid, u16 addr, const u8 *buf, size_t len)
122{
123	int ret;
124
125	if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
126		return -EINVAL;
127
128	trace_spmi_write_begin(opcode, sid, addr, len, buf);
129	ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
130	trace_spmi_write_end(opcode, sid, addr, ret);
131	return ret;
132}
133
134/**
135 * spmi_register_read() - register read
136 * @sdev:	SPMI device.
137 * @addr:	slave register address (5-bit address).
138 * @buf:	buffer to be populated with data from the Slave.
139 *
140 * Reads 1 byte of data from a Slave device register.
141 */
142int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
143{
144	/* 5-bit register address */
145	if (addr > 0x1F)
146		return -EINVAL;
147
148	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
149			     buf, 1);
150}
151EXPORT_SYMBOL_GPL(spmi_register_read);
152
153/**
154 * spmi_ext_register_read() - extended register read
155 * @sdev:	SPMI device.
156 * @addr:	slave register address (8-bit address).
157 * @buf:	buffer to be populated with data from the Slave.
158 * @len:	the request number of bytes to read (up to 16 bytes).
159 *
160 * Reads up to 16 bytes of data from the extended register space on a
161 * Slave device.
162 */
163int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
164			   size_t len)
165{
166	/* 8-bit register address, up to 16 bytes */
167	if (len == 0 || len > 16)
168		return -EINVAL;
169
170	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
171			     buf, len);
172}
173EXPORT_SYMBOL_GPL(spmi_ext_register_read);
174
175/**
176 * spmi_ext_register_readl() - extended register read long
177 * @sdev:	SPMI device.
178 * @addr:	slave register address (16-bit address).
179 * @buf:	buffer to be populated with data from the Slave.
180 * @len:	the request number of bytes to read (up to 8 bytes).
181 *
182 * Reads up to 8 bytes of data from the extended register space on a
183 * Slave device using 16-bit address.
184 */
185int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
186			    size_t len)
187{
188	/* 16-bit register address, up to 8 bytes */
189	if (len == 0 || len > 8)
190		return -EINVAL;
191
192	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
193			     buf, len);
194}
195EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
196
197/**
198 * spmi_register_write() - register write
199 * @sdev:	SPMI device
200 * @addr:	slave register address (5-bit address).
201 * @data:	buffer containing the data to be transferred to the Slave.
202 *
203 * Writes 1 byte of data to a Slave device register.
204 */
205int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
206{
207	/* 5-bit register address */
208	if (addr > 0x1F)
209		return -EINVAL;
210
211	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
212			      &data, 1);
213}
214EXPORT_SYMBOL_GPL(spmi_register_write);
215
216/**
217 * spmi_register_zero_write() - register zero write
218 * @sdev:	SPMI device.
219 * @data:	the data to be written to register 0 (7-bits).
220 *
221 * Writes data to register 0 of the Slave device.
222 */
223int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
224{
225	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
226			      &data, 1);
227}
228EXPORT_SYMBOL_GPL(spmi_register_zero_write);
229
230/**
231 * spmi_ext_register_write() - extended register write
232 * @sdev:	SPMI device.
233 * @addr:	slave register address (8-bit address).
234 * @buf:	buffer containing the data to be transferred to the Slave.
235 * @len:	the request number of bytes to read (up to 16 bytes).
236 *
237 * Writes up to 16 bytes of data to the extended register space of a
238 * Slave device.
239 */
240int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
241			    size_t len)
242{
243	/* 8-bit register address, up to 16 bytes */
244	if (len == 0 || len > 16)
245		return -EINVAL;
246
247	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
248			      buf, len);
249}
250EXPORT_SYMBOL_GPL(spmi_ext_register_write);
251
252/**
253 * spmi_ext_register_writel() - extended register write long
254 * @sdev:	SPMI device.
255 * @addr:	slave register address (16-bit address).
256 * @buf:	buffer containing the data to be transferred to the Slave.
257 * @len:	the request number of bytes to read (up to 8 bytes).
258 *
259 * Writes up to 8 bytes of data to the extended register space of a
260 * Slave device using 16-bit address.
261 */
262int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
263			     size_t len)
264{
265	/* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
266	if (len == 0 || len > 8)
267		return -EINVAL;
268
269	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
270			      addr, buf, len);
271}
272EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
273
274/**
275 * spmi_command_reset() - sends RESET command to the specified slave
276 * @sdev:	SPMI device.
277 *
278 * The Reset command initializes the Slave and forces all registers to
279 * their reset values. The Slave shall enter the STARTUP state after
280 * receiving a Reset command.
281 */
282int spmi_command_reset(struct spmi_device *sdev)
283{
284	return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
285}
286EXPORT_SYMBOL_GPL(spmi_command_reset);
287
288/**
289 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
290 * @sdev:	SPMI device.
291 *
292 * The Sleep command causes the Slave to enter the user defined SLEEP state.
293 */
294int spmi_command_sleep(struct spmi_device *sdev)
295{
296	return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
297}
298EXPORT_SYMBOL_GPL(spmi_command_sleep);
299
300/**
301 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
302 * @sdev:	SPMI device.
303 *
304 * The Wakeup command causes the Slave to move from the SLEEP state to
305 * the ACTIVE state.
306 */
307int spmi_command_wakeup(struct spmi_device *sdev)
308{
309	return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
310}
311EXPORT_SYMBOL_GPL(spmi_command_wakeup);
312
313/**
314 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
315 * @sdev:	SPMI device.
316 *
317 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
318 */
319int spmi_command_shutdown(struct spmi_device *sdev)
320{
321	return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
322}
323EXPORT_SYMBOL_GPL(spmi_command_shutdown);
324
325static int spmi_drv_probe(struct device *dev)
326{
327	const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
328	struct spmi_device *sdev = to_spmi_device(dev);
329	int err;
330
331	pm_runtime_get_noresume(dev);
332	pm_runtime_set_active(dev);
333	pm_runtime_enable(dev);
334
335	err = sdrv->probe(sdev);
336	if (err)
337		goto fail_probe;
338
339	return 0;
340
341fail_probe:
342	pm_runtime_disable(dev);
343	pm_runtime_set_suspended(dev);
344	pm_runtime_put_noidle(dev);
345	return err;
346}
347
348static void spmi_drv_remove(struct device *dev)
349{
350	const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
351
352	pm_runtime_get_sync(dev);
353	sdrv->remove(to_spmi_device(dev));
354	pm_runtime_put_noidle(dev);
355
356	pm_runtime_disable(dev);
357	pm_runtime_set_suspended(dev);
358	pm_runtime_put_noidle(dev);
359}
360
361static void spmi_drv_shutdown(struct device *dev)
362{
363	const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
364
365	if (sdrv && sdrv->shutdown)
366		sdrv->shutdown(to_spmi_device(dev));
367}
368
369static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
370{
371	int ret;
372
373	ret = of_device_uevent_modalias(dev, env);
374	if (ret != -ENODEV)
375		return ret;
376
377	return 0;
378}
379
380static struct bus_type spmi_bus_type = {
381	.name		= "spmi",
382	.match		= spmi_device_match,
383	.probe		= spmi_drv_probe,
384	.remove		= spmi_drv_remove,
385	.shutdown	= spmi_drv_shutdown,
386	.uevent		= spmi_drv_uevent,
387};
388
389/**
390 * spmi_device_from_of() - get the associated SPMI device from a device node
391 *
392 * @np:		device node
393 *
394 * Returns the struct spmi_device associated with a device node or NULL.
395 */
396struct spmi_device *spmi_device_from_of(struct device_node *np)
397{
398	struct device *dev = bus_find_device_by_of_node(&spmi_bus_type, np);
399
400	if (dev)
401		return to_spmi_device(dev);
402	return NULL;
403}
404EXPORT_SYMBOL_GPL(spmi_device_from_of);
405
406/**
407 * spmi_controller_alloc() - Allocate a new SPMI device
408 * @ctrl:	associated controller
409 *
410 * Caller is responsible for either calling spmi_device_add() to add the
411 * newly allocated controller, or calling spmi_device_put() to discard it.
412 */
413struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
414{
415	struct spmi_device *sdev;
416
417	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
418	if (!sdev)
419		return NULL;
420
421	sdev->ctrl = ctrl;
422	device_initialize(&sdev->dev);
423	sdev->dev.parent = &ctrl->dev;
424	sdev->dev.bus = &spmi_bus_type;
425	sdev->dev.type = &spmi_dev_type;
426	return sdev;
427}
428EXPORT_SYMBOL_GPL(spmi_device_alloc);
429
430/**
431 * spmi_controller_alloc() - Allocate a new SPMI controller
432 * @parent:	parent device
433 * @size:	size of private data
434 *
435 * Caller is responsible for either calling spmi_controller_add() to add the
436 * newly allocated controller, or calling spmi_controller_put() to discard it.
437 * The allocated private data region may be accessed via
438 * spmi_controller_get_drvdata()
439 */
440struct spmi_controller *spmi_controller_alloc(struct device *parent,
441					      size_t size)
442{
443	struct spmi_controller *ctrl;
444	int id;
445
446	if (WARN_ON(!parent))
447		return NULL;
448
449	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
450	if (!ctrl)
451		return NULL;
452
453	device_initialize(&ctrl->dev);
454	ctrl->dev.type = &spmi_ctrl_type;
455	ctrl->dev.bus = &spmi_bus_type;
456	ctrl->dev.parent = parent;
457	ctrl->dev.of_node = parent->of_node;
458	spmi_controller_set_drvdata(ctrl, &ctrl[1]);
459
460	id = ida_alloc(&ctrl_ida, GFP_KERNEL);
461	if (id < 0) {
462		dev_err(parent,
463			"unable to allocate SPMI controller identifier.\n");
464		spmi_controller_put(ctrl);
465		return NULL;
466	}
467
468	ctrl->nr = id;
469	dev_set_name(&ctrl->dev, "spmi-%d", id);
470
471	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
472	return ctrl;
473}
474EXPORT_SYMBOL_GPL(spmi_controller_alloc);
475
476static void of_spmi_register_devices(struct spmi_controller *ctrl)
477{
478	struct device_node *node;
479	int err;
480
481	if (!ctrl->dev.of_node)
482		return;
483
484	for_each_available_child_of_node(ctrl->dev.of_node, node) {
485		struct spmi_device *sdev;
486		u32 reg[2];
487
488		dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
489
490		err = of_property_read_u32_array(node, "reg", reg, 2);
491		if (err) {
492			dev_err(&ctrl->dev,
493				"node %pOF err (%d) does not have 'reg' property\n",
494				node, err);
495			continue;
496		}
497
498		if (reg[1] != SPMI_USID) {
499			dev_err(&ctrl->dev,
500				"node %pOF contains unsupported 'reg' entry\n",
501				node);
502			continue;
503		}
504
505		if (reg[0] >= SPMI_MAX_SLAVE_ID) {
506			dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
 
 
507			continue;
508		}
509
510		dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
511
512		sdev = spmi_device_alloc(ctrl);
513		if (!sdev)
514			continue;
515
516		sdev->dev.of_node = node;
517		sdev->usid = (u8)reg[0];
518
519		err = spmi_device_add(sdev);
520		if (err) {
521			dev_err(&sdev->dev,
522				"failure adding device. status %d\n", err);
523			spmi_device_put(sdev);
524		}
525	}
526}
527
528/**
529 * spmi_controller_add() - Add an SPMI controller
530 * @ctrl:	controller to be registered.
531 *
532 * Register a controller previously allocated via spmi_controller_alloc() with
533 * the SPMI core.
534 */
535int spmi_controller_add(struct spmi_controller *ctrl)
536{
537	int ret;
538
539	/* Can't register until after driver model init */
540	if (WARN_ON(!is_registered))
541		return -EAGAIN;
542
543	ret = device_add(&ctrl->dev);
544	if (ret)
545		return ret;
546
547	if (IS_ENABLED(CONFIG_OF))
548		of_spmi_register_devices(ctrl);
549
550	dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
551		ctrl->nr, &ctrl->dev);
552
553	return 0;
554};
555EXPORT_SYMBOL_GPL(spmi_controller_add);
556
557/* Remove a device associated with a controller */
558static int spmi_ctrl_remove_device(struct device *dev, void *data)
559{
560	struct spmi_device *spmidev = to_spmi_device(dev);
561
562	if (dev->type == &spmi_dev_type)
563		spmi_device_remove(spmidev);
564	return 0;
565}
566
567/**
568 * spmi_controller_remove(): remove an SPMI controller
569 * @ctrl:	controller to remove
570 *
571 * Remove a SPMI controller.  Caller is responsible for calling
572 * spmi_controller_put() to discard the allocated controller.
573 */
574void spmi_controller_remove(struct spmi_controller *ctrl)
575{
 
 
576	if (!ctrl)
577		return;
578
579	device_for_each_child(&ctrl->dev, NULL, spmi_ctrl_remove_device);
 
580	device_del(&ctrl->dev);
581}
582EXPORT_SYMBOL_GPL(spmi_controller_remove);
583
584/**
585 * spmi_driver_register() - Register client driver with SPMI core
586 * @sdrv:	client driver to be associated with client-device.
587 *
588 * This API will register the client driver with the SPMI framework.
589 * It is typically called from the driver's module-init function.
590 */
591int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
592{
593	sdrv->driver.bus = &spmi_bus_type;
594	sdrv->driver.owner = owner;
595	return driver_register(&sdrv->driver);
596}
597EXPORT_SYMBOL_GPL(__spmi_driver_register);
598
599static void __exit spmi_exit(void)
600{
601	bus_unregister(&spmi_bus_type);
602}
603module_exit(spmi_exit);
604
605static int __init spmi_init(void)
606{
607	int ret;
608
609	ret = bus_register(&spmi_bus_type);
610	if (ret)
611		return ret;
612
613	is_registered = true;
614	return 0;
615}
616postcore_initcall(spmi_init);
617
618MODULE_LICENSE("GPL v2");
619MODULE_DESCRIPTION("SPMI module");
620MODULE_ALIAS("platform:spmi");
v4.6
 
  1/*
  2 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License version 2 and
  6 * only version 2 as published by the Free Software Foundation.
  7 *
  8 * This program is distributed in the hope that it will be useful,
  9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11 * GNU General Public License for more details.
 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#define CREATE_TRACE_POINTS
 26#include <trace/events/spmi.h>
 27
 
 28static DEFINE_IDA(ctrl_ida);
 29
 30static void spmi_dev_release(struct device *dev)
 31{
 32	struct spmi_device *sdev = to_spmi_device(dev);
 
 33	kfree(sdev);
 34}
 35
 36static const struct device_type spmi_dev_type = {
 37	.release	= spmi_dev_release,
 38};
 39
 40static void spmi_ctrl_release(struct device *dev)
 41{
 42	struct spmi_controller *ctrl = to_spmi_controller(dev);
 43	ida_simple_remove(&ctrl_ida, ctrl->nr);
 
 44	kfree(ctrl);
 45}
 46
 47static const struct device_type spmi_ctrl_type = {
 48	.release	= spmi_ctrl_release,
 49};
 50
 51static int spmi_device_match(struct device *dev, struct device_driver *drv)
 52{
 53	if (of_driver_match_device(dev, drv))
 54		return 1;
 55
 56	if (drv->name)
 57		return strncmp(dev_name(dev), drv->name,
 58			       SPMI_NAME_SIZE) == 0;
 59
 60	return 0;
 61}
 62
 63/**
 64 * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
 65 * @sdev:	spmi_device to be added
 66 */
 67int spmi_device_add(struct spmi_device *sdev)
 68{
 69	struct spmi_controller *ctrl = sdev->ctrl;
 70	int err;
 71
 72	dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
 73
 74	err = device_add(&sdev->dev);
 75	if (err < 0) {
 76		dev_err(&sdev->dev, "Can't add %s, status %d\n",
 77			dev_name(&sdev->dev), err);
 78		goto err_device_add;
 79	}
 80
 81	dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
 82
 83err_device_add:
 84	return err;
 85}
 86EXPORT_SYMBOL_GPL(spmi_device_add);
 87
 88/**
 89 * spmi_device_remove(): remove an SPMI device
 90 * @sdev:	spmi_device to be removed
 91 */
 92void spmi_device_remove(struct spmi_device *sdev)
 93{
 94	device_unregister(&sdev->dev);
 95}
 96EXPORT_SYMBOL_GPL(spmi_device_remove);
 97
 98static inline int
 99spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
100{
101	int ret;
102
103	if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
104		return -EINVAL;
105
106	ret = ctrl->cmd(ctrl, opcode, sid);
107	trace_spmi_cmd(opcode, sid, ret);
108	return ret;
109}
110
111static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
112				u8 sid, u16 addr, u8 *buf, size_t len)
113{
114	int ret;
115
116	if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
117		return -EINVAL;
118
119	trace_spmi_read_begin(opcode, sid, addr);
120	ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
121	trace_spmi_read_end(opcode, sid, addr, ret, len, buf);
122	return ret;
123}
124
125static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
126				 u8 sid, u16 addr, const u8 *buf, size_t len)
127{
128	int ret;
129
130	if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
131		return -EINVAL;
132
133	trace_spmi_write_begin(opcode, sid, addr, len, buf);
134	ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
135	trace_spmi_write_end(opcode, sid, addr, ret);
136	return ret;
137}
138
139/**
140 * spmi_register_read() - register read
141 * @sdev:	SPMI device.
142 * @addr:	slave register address (5-bit address).
143 * @buf:	buffer to be populated with data from the Slave.
144 *
145 * Reads 1 byte of data from a Slave device register.
146 */
147int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
148{
149	/* 5-bit register address */
150	if (addr > 0x1F)
151		return -EINVAL;
152
153	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
154			     buf, 1);
155}
156EXPORT_SYMBOL_GPL(spmi_register_read);
157
158/**
159 * spmi_ext_register_read() - extended register read
160 * @sdev:	SPMI device.
161 * @addr:	slave register address (8-bit address).
162 * @buf:	buffer to be populated with data from the Slave.
163 * @len:	the request number of bytes to read (up to 16 bytes).
164 *
165 * Reads up to 16 bytes of data from the extended register space on a
166 * Slave device.
167 */
168int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
169			   size_t len)
170{
171	/* 8-bit register address, up to 16 bytes */
172	if (len == 0 || len > 16)
173		return -EINVAL;
174
175	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
176			     buf, len);
177}
178EXPORT_SYMBOL_GPL(spmi_ext_register_read);
179
180/**
181 * spmi_ext_register_readl() - extended register read long
182 * @sdev:	SPMI device.
183 * @addr:	slave register address (16-bit address).
184 * @buf:	buffer to be populated with data from the Slave.
185 * @len:	the request number of bytes to read (up to 8 bytes).
186 *
187 * Reads up to 8 bytes of data from the extended register space on a
188 * Slave device using 16-bit address.
189 */
190int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
191			    size_t len)
192{
193	/* 16-bit register address, up to 8 bytes */
194	if (len == 0 || len > 8)
195		return -EINVAL;
196
197	return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
198			     buf, len);
199}
200EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
201
202/**
203 * spmi_register_write() - register write
204 * @sdev:	SPMI device
205 * @addr:	slave register address (5-bit address).
206 * @data:	buffer containing the data to be transferred to the Slave.
207 *
208 * Writes 1 byte of data to a Slave device register.
209 */
210int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
211{
212	/* 5-bit register address */
213	if (addr > 0x1F)
214		return -EINVAL;
215
216	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
217			      &data, 1);
218}
219EXPORT_SYMBOL_GPL(spmi_register_write);
220
221/**
222 * spmi_register_zero_write() - register zero write
223 * @sdev:	SPMI device.
224 * @data:	the data to be written to register 0 (7-bits).
225 *
226 * Writes data to register 0 of the Slave device.
227 */
228int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
229{
230	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
231			      &data, 1);
232}
233EXPORT_SYMBOL_GPL(spmi_register_zero_write);
234
235/**
236 * spmi_ext_register_write() - extended register write
237 * @sdev:	SPMI device.
238 * @addr:	slave register address (8-bit address).
239 * @buf:	buffer containing the data to be transferred to the Slave.
240 * @len:	the request number of bytes to read (up to 16 bytes).
241 *
242 * Writes up to 16 bytes of data to the extended register space of a
243 * Slave device.
244 */
245int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
246			    size_t len)
247{
248	/* 8-bit register address, up to 16 bytes */
249	if (len == 0 || len > 16)
250		return -EINVAL;
251
252	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
253			      buf, len);
254}
255EXPORT_SYMBOL_GPL(spmi_ext_register_write);
256
257/**
258 * spmi_ext_register_writel() - extended register write long
259 * @sdev:	SPMI device.
260 * @addr:	slave register address (16-bit address).
261 * @buf:	buffer containing the data to be transferred to the Slave.
262 * @len:	the request number of bytes to read (up to 8 bytes).
263 *
264 * Writes up to 8 bytes of data to the extended register space of a
265 * Slave device using 16-bit address.
266 */
267int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
268			     size_t len)
269{
270	/* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
271	if (len == 0 || len > 8)
272		return -EINVAL;
273
274	return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
275			      addr, buf, len);
276}
277EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
278
279/**
280 * spmi_command_reset() - sends RESET command to the specified slave
281 * @sdev:	SPMI device.
282 *
283 * The Reset command initializes the Slave and forces all registers to
284 * their reset values. The Slave shall enter the STARTUP state after
285 * receiving a Reset command.
286 */
287int spmi_command_reset(struct spmi_device *sdev)
288{
289	return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
290}
291EXPORT_SYMBOL_GPL(spmi_command_reset);
292
293/**
294 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
295 * @sdev:	SPMI device.
296 *
297 * The Sleep command causes the Slave to enter the user defined SLEEP state.
298 */
299int spmi_command_sleep(struct spmi_device *sdev)
300{
301	return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
302}
303EXPORT_SYMBOL_GPL(spmi_command_sleep);
304
305/**
306 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
307 * @sdev:	SPMI device.
308 *
309 * The Wakeup command causes the Slave to move from the SLEEP state to
310 * the ACTIVE state.
311 */
312int spmi_command_wakeup(struct spmi_device *sdev)
313{
314	return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
315}
316EXPORT_SYMBOL_GPL(spmi_command_wakeup);
317
318/**
319 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
320 * @sdev:	SPMI device.
321 *
322 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
323 */
324int spmi_command_shutdown(struct spmi_device *sdev)
325{
326	return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
327}
328EXPORT_SYMBOL_GPL(spmi_command_shutdown);
329
330static int spmi_drv_probe(struct device *dev)
331{
332	const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
333	struct spmi_device *sdev = to_spmi_device(dev);
334	int err;
335
336	pm_runtime_get_noresume(dev);
337	pm_runtime_set_active(dev);
338	pm_runtime_enable(dev);
339
340	err = sdrv->probe(sdev);
341	if (err)
342		goto fail_probe;
343
344	return 0;
345
346fail_probe:
347	pm_runtime_disable(dev);
348	pm_runtime_set_suspended(dev);
349	pm_runtime_put_noidle(dev);
350	return err;
351}
352
353static int spmi_drv_remove(struct device *dev)
354{
355	const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
356
357	pm_runtime_get_sync(dev);
358	sdrv->remove(to_spmi_device(dev));
359	pm_runtime_put_noidle(dev);
360
361	pm_runtime_disable(dev);
362	pm_runtime_set_suspended(dev);
363	pm_runtime_put_noidle(dev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364	return 0;
365}
366
367static struct bus_type spmi_bus_type = {
368	.name		= "spmi",
369	.match		= spmi_device_match,
370	.probe		= spmi_drv_probe,
371	.remove		= spmi_drv_remove,
 
 
372};
373
374/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
375 * spmi_controller_alloc() - Allocate a new SPMI device
376 * @ctrl:	associated controller
377 *
378 * Caller is responsible for either calling spmi_device_add() to add the
379 * newly allocated controller, or calling spmi_device_put() to discard it.
380 */
381struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
382{
383	struct spmi_device *sdev;
384
385	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
386	if (!sdev)
387		return NULL;
388
389	sdev->ctrl = ctrl;
390	device_initialize(&sdev->dev);
391	sdev->dev.parent = &ctrl->dev;
392	sdev->dev.bus = &spmi_bus_type;
393	sdev->dev.type = &spmi_dev_type;
394	return sdev;
395}
396EXPORT_SYMBOL_GPL(spmi_device_alloc);
397
398/**
399 * spmi_controller_alloc() - Allocate a new SPMI controller
400 * @parent:	parent device
401 * @size:	size of private data
402 *
403 * Caller is responsible for either calling spmi_controller_add() to add the
404 * newly allocated controller, or calling spmi_controller_put() to discard it.
405 * The allocated private data region may be accessed via
406 * spmi_controller_get_drvdata()
407 */
408struct spmi_controller *spmi_controller_alloc(struct device *parent,
409					      size_t size)
410{
411	struct spmi_controller *ctrl;
412	int id;
413
414	if (WARN_ON(!parent))
415		return NULL;
416
417	ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
418	if (!ctrl)
419		return NULL;
420
421	device_initialize(&ctrl->dev);
422	ctrl->dev.type = &spmi_ctrl_type;
423	ctrl->dev.bus = &spmi_bus_type;
424	ctrl->dev.parent = parent;
425	ctrl->dev.of_node = parent->of_node;
426	spmi_controller_set_drvdata(ctrl, &ctrl[1]);
427
428	id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
429	if (id < 0) {
430		dev_err(parent,
431			"unable to allocate SPMI controller identifier.\n");
432		spmi_controller_put(ctrl);
433		return NULL;
434	}
435
436	ctrl->nr = id;
437	dev_set_name(&ctrl->dev, "spmi-%d", id);
438
439	dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
440	return ctrl;
441}
442EXPORT_SYMBOL_GPL(spmi_controller_alloc);
443
444static void of_spmi_register_devices(struct spmi_controller *ctrl)
445{
446	struct device_node *node;
447	int err;
448
449	if (!ctrl->dev.of_node)
450		return;
451
452	for_each_available_child_of_node(ctrl->dev.of_node, node) {
453		struct spmi_device *sdev;
454		u32 reg[2];
455
456		dev_dbg(&ctrl->dev, "adding child %s\n", node->full_name);
457
458		err = of_property_read_u32_array(node, "reg", reg, 2);
459		if (err) {
460			dev_err(&ctrl->dev,
461				"node %s err (%d) does not have 'reg' property\n",
462				node->full_name, err);
463			continue;
464		}
465
466		if (reg[1] != SPMI_USID) {
467			dev_err(&ctrl->dev,
468				"node %s contains unsupported 'reg' entry\n",
469				node->full_name);
470			continue;
471		}
472
473		if (reg[0] >= SPMI_MAX_SLAVE_ID) {
474			dev_err(&ctrl->dev,
475				"invalid usid on node %s\n",
476				node->full_name);
477			continue;
478		}
479
480		dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
481
482		sdev = spmi_device_alloc(ctrl);
483		if (!sdev)
484			continue;
485
486		sdev->dev.of_node = node;
487		sdev->usid = (u8) reg[0];
488
489		err = spmi_device_add(sdev);
490		if (err) {
491			dev_err(&sdev->dev,
492				"failure adding device. status %d\n", err);
493			spmi_device_put(sdev);
494		}
495	}
496}
497
498/**
499 * spmi_controller_add() - Add an SPMI controller
500 * @ctrl:	controller to be registered.
501 *
502 * Register a controller previously allocated via spmi_controller_alloc() with
503 * the SPMI core.
504 */
505int spmi_controller_add(struct spmi_controller *ctrl)
506{
507	int ret;
508
509	/* Can't register until after driver model init */
510	if (WARN_ON(!spmi_bus_type.p))
511		return -EAGAIN;
512
513	ret = device_add(&ctrl->dev);
514	if (ret)
515		return ret;
516
517	if (IS_ENABLED(CONFIG_OF))
518		of_spmi_register_devices(ctrl);
519
520	dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
521		ctrl->nr, &ctrl->dev);
522
523	return 0;
524};
525EXPORT_SYMBOL_GPL(spmi_controller_add);
526
527/* Remove a device associated with a controller */
528static int spmi_ctrl_remove_device(struct device *dev, void *data)
529{
530	struct spmi_device *spmidev = to_spmi_device(dev);
 
531	if (dev->type == &spmi_dev_type)
532		spmi_device_remove(spmidev);
533	return 0;
534}
535
536/**
537 * spmi_controller_remove(): remove an SPMI controller
538 * @ctrl:	controller to remove
539 *
540 * Remove a SPMI controller.  Caller is responsible for calling
541 * spmi_controller_put() to discard the allocated controller.
542 */
543void spmi_controller_remove(struct spmi_controller *ctrl)
544{
545	int dummy;
546
547	if (!ctrl)
548		return;
549
550	dummy = device_for_each_child(&ctrl->dev, NULL,
551				      spmi_ctrl_remove_device);
552	device_del(&ctrl->dev);
553}
554EXPORT_SYMBOL_GPL(spmi_controller_remove);
555
556/**
557 * spmi_driver_register() - Register client driver with SPMI core
558 * @sdrv:	client driver to be associated with client-device.
559 *
560 * This API will register the client driver with the SPMI framework.
561 * It is typically called from the driver's module-init function.
562 */
563int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
564{
565	sdrv->driver.bus = &spmi_bus_type;
566	sdrv->driver.owner = owner;
567	return driver_register(&sdrv->driver);
568}
569EXPORT_SYMBOL_GPL(__spmi_driver_register);
570
571static void __exit spmi_exit(void)
572{
573	bus_unregister(&spmi_bus_type);
574}
575module_exit(spmi_exit);
576
577static int __init spmi_init(void)
578{
579	return bus_register(&spmi_bus_type);
 
 
 
 
 
 
 
580}
581postcore_initcall(spmi_init);
582
583MODULE_LICENSE("GPL v2");
584MODULE_DESCRIPTION("SPMI module");
585MODULE_ALIAS("platform:spmi");