xref: /linux-6.15/include/linux/spmi.h (revision b6e53731)
197fb5e8dSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
25a86bf34SKenneth Heitke /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
35a86bf34SKenneth Heitke  */
45a86bf34SKenneth Heitke #ifndef _LINUX_SPMI_H
55a86bf34SKenneth Heitke #define _LINUX_SPMI_H
65a86bf34SKenneth Heitke 
75a86bf34SKenneth Heitke #include <linux/types.h>
85a86bf34SKenneth Heitke #include <linux/device.h>
95a86bf34SKenneth Heitke #include <linux/mod_devicetable.h>
105a86bf34SKenneth Heitke 
115a86bf34SKenneth Heitke /* Maximum slave identifier */
125a86bf34SKenneth Heitke #define SPMI_MAX_SLAVE_ID		16
135a86bf34SKenneth Heitke 
145a86bf34SKenneth Heitke /* SPMI Commands */
155a86bf34SKenneth Heitke #define SPMI_CMD_EXT_WRITE		0x00
165a86bf34SKenneth Heitke #define SPMI_CMD_RESET			0x10
175a86bf34SKenneth Heitke #define SPMI_CMD_SLEEP			0x11
185a86bf34SKenneth Heitke #define SPMI_CMD_SHUTDOWN		0x12
195a86bf34SKenneth Heitke #define SPMI_CMD_WAKEUP			0x13
205a86bf34SKenneth Heitke #define SPMI_CMD_AUTHENTICATE		0x14
215a86bf34SKenneth Heitke #define SPMI_CMD_MSTR_READ		0x15
225a86bf34SKenneth Heitke #define SPMI_CMD_MSTR_WRITE		0x16
235a86bf34SKenneth Heitke #define SPMI_CMD_TRANSFER_BUS_OWNERSHIP	0x1A
245a86bf34SKenneth Heitke #define SPMI_CMD_DDB_MASTER_READ	0x1B
255a86bf34SKenneth Heitke #define SPMI_CMD_DDB_SLAVE_READ		0x1C
265a86bf34SKenneth Heitke #define SPMI_CMD_EXT_READ		0x20
275a86bf34SKenneth Heitke #define SPMI_CMD_EXT_WRITEL		0x30
285a86bf34SKenneth Heitke #define SPMI_CMD_EXT_READL		0x38
295a86bf34SKenneth Heitke #define SPMI_CMD_WRITE			0x40
305a86bf34SKenneth Heitke #define SPMI_CMD_READ			0x60
315a86bf34SKenneth Heitke #define SPMI_CMD_ZERO_WRITE		0x80
325a86bf34SKenneth Heitke 
335a86bf34SKenneth Heitke /**
345a86bf34SKenneth Heitke  * struct spmi_device - Basic representation of an SPMI device
355a86bf34SKenneth Heitke  * @dev:	Driver model representation of the device.
365a86bf34SKenneth Heitke  * @ctrl:	SPMI controller managing the bus hosting this device.
375a86bf34SKenneth Heitke  * @usid:	This devices' Unique Slave IDentifier.
385a86bf34SKenneth Heitke  */
395a86bf34SKenneth Heitke struct spmi_device {
405a86bf34SKenneth Heitke 	struct device		dev;
415a86bf34SKenneth Heitke 	struct spmi_controller	*ctrl;
425a86bf34SKenneth Heitke 	u8			usid;
435a86bf34SKenneth Heitke };
445a86bf34SKenneth Heitke 
to_spmi_device(struct device * d)455a86bf34SKenneth Heitke static inline struct spmi_device *to_spmi_device(struct device *d)
465a86bf34SKenneth Heitke {
475a86bf34SKenneth Heitke 	return container_of(d, struct spmi_device, dev);
485a86bf34SKenneth Heitke }
495a86bf34SKenneth Heitke 
spmi_device_get_drvdata(const struct spmi_device * sdev)505a86bf34SKenneth Heitke static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
515a86bf34SKenneth Heitke {
525a86bf34SKenneth Heitke 	return dev_get_drvdata(&sdev->dev);
535a86bf34SKenneth Heitke }
545a86bf34SKenneth Heitke 
spmi_device_set_drvdata(struct spmi_device * sdev,void * data)555a86bf34SKenneth Heitke static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
565a86bf34SKenneth Heitke {
575a86bf34SKenneth Heitke 	dev_set_drvdata(&sdev->dev, data);
585a86bf34SKenneth Heitke }
595a86bf34SKenneth Heitke 
605a86bf34SKenneth Heitke struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
615a86bf34SKenneth Heitke 
spmi_device_put(struct spmi_device * sdev)625a86bf34SKenneth Heitke static inline void spmi_device_put(struct spmi_device *sdev)
635a86bf34SKenneth Heitke {
645a86bf34SKenneth Heitke 	if (sdev)
655a86bf34SKenneth Heitke 		put_device(&sdev->dev);
665a86bf34SKenneth Heitke }
675a86bf34SKenneth Heitke 
685a86bf34SKenneth Heitke int spmi_device_add(struct spmi_device *sdev);
695a86bf34SKenneth Heitke 
705a86bf34SKenneth Heitke void spmi_device_remove(struct spmi_device *sdev);
715a86bf34SKenneth Heitke 
725a86bf34SKenneth Heitke /**
735a86bf34SKenneth Heitke  * struct spmi_controller - interface to the SPMI master controller
745a86bf34SKenneth Heitke  * @dev:	Driver model representation of the device.
755a86bf34SKenneth Heitke  * @nr:		board-specific number identifier for this controller/bus
765a86bf34SKenneth Heitke  * @cmd:	sends a non-data command sequence on the SPMI bus.
775a86bf34SKenneth Heitke  * @read_cmd:	sends a register read command sequence on the SPMI bus.
785a86bf34SKenneth Heitke  * @write_cmd:	sends a register write command sequence on the SPMI bus.
795a86bf34SKenneth Heitke  */
805a86bf34SKenneth Heitke struct spmi_controller {
815a86bf34SKenneth Heitke 	struct device		dev;
825a86bf34SKenneth Heitke 	unsigned int		nr;
835a86bf34SKenneth Heitke 	int	(*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
845a86bf34SKenneth Heitke 	int	(*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
855a86bf34SKenneth Heitke 			    u8 sid, u16 addr, u8 *buf, size_t len);
865a86bf34SKenneth Heitke 	int	(*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
875a86bf34SKenneth Heitke 			     u8 sid, u16 addr, const u8 *buf, size_t len);
885a86bf34SKenneth Heitke };
895a86bf34SKenneth Heitke 
to_spmi_controller(struct device * d)905a86bf34SKenneth Heitke static inline struct spmi_controller *to_spmi_controller(struct device *d)
915a86bf34SKenneth Heitke {
925a86bf34SKenneth Heitke 	return container_of(d, struct spmi_controller, dev);
935a86bf34SKenneth Heitke }
945a86bf34SKenneth Heitke 
955a86bf34SKenneth Heitke static inline
spmi_controller_get_drvdata(const struct spmi_controller * ctrl)965a86bf34SKenneth Heitke void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
975a86bf34SKenneth Heitke {
985a86bf34SKenneth Heitke 	return dev_get_drvdata(&ctrl->dev);
995a86bf34SKenneth Heitke }
1005a86bf34SKenneth Heitke 
spmi_controller_set_drvdata(struct spmi_controller * ctrl,void * data)1015a86bf34SKenneth Heitke static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
1025a86bf34SKenneth Heitke 					       void *data)
1035a86bf34SKenneth Heitke {
1045a86bf34SKenneth Heitke 	dev_set_drvdata(&ctrl->dev, data);
1055a86bf34SKenneth Heitke }
1065a86bf34SKenneth Heitke 
1075a86bf34SKenneth Heitke struct spmi_controller *spmi_controller_alloc(struct device *parent,
1085a86bf34SKenneth Heitke 					      size_t size);
1095a86bf34SKenneth Heitke 
1105a86bf34SKenneth Heitke /**
1115a86bf34SKenneth Heitke  * spmi_controller_put() - decrement controller refcount
1125a86bf34SKenneth Heitke  * @ctrl	SPMI controller.
1135a86bf34SKenneth Heitke  */
spmi_controller_put(struct spmi_controller * ctrl)1145a86bf34SKenneth Heitke static inline void spmi_controller_put(struct spmi_controller *ctrl)
1155a86bf34SKenneth Heitke {
1165a86bf34SKenneth Heitke 	if (ctrl)
1175a86bf34SKenneth Heitke 		put_device(&ctrl->dev);
1185a86bf34SKenneth Heitke }
1195a86bf34SKenneth Heitke 
1205a86bf34SKenneth Heitke int spmi_controller_add(struct spmi_controller *ctrl);
1215a86bf34SKenneth Heitke void spmi_controller_remove(struct spmi_controller *ctrl);
1225a86bf34SKenneth Heitke 
123*b6e53731SFei Shao struct spmi_controller *devm_spmi_controller_alloc(struct device *parent, size_t size);
124*b6e53731SFei Shao int devm_spmi_controller_add(struct device *parent, struct spmi_controller *ctrl);
125*b6e53731SFei Shao 
1265a86bf34SKenneth Heitke /**
1275a86bf34SKenneth Heitke  * struct spmi_driver - SPMI slave device driver
1285a86bf34SKenneth Heitke  * @driver:	SPMI device drivers should initialize name and owner field of
1295a86bf34SKenneth Heitke  *		this structure.
1305a86bf34SKenneth Heitke  * @probe:	binds this driver to a SPMI device.
1315a86bf34SKenneth Heitke  * @remove:	unbinds this driver from the SPMI device.
1325a86bf34SKenneth Heitke  *
1335a86bf34SKenneth Heitke  * If PM runtime support is desired for a slave, a device driver can call
1345a86bf34SKenneth Heitke  * pm_runtime_put() from their probe() routine (and a balancing
1355a86bf34SKenneth Heitke  * pm_runtime_get() in remove()).  PM runtime support for a slave is
1365a86bf34SKenneth Heitke  * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
1375a86bf34SKenneth Heitke  * transitioning the slave into the SLEEP state.  On runtime_resume(), a WAKEUP
1385a86bf34SKenneth Heitke  * command is sent to the slave to bring it back to ACTIVE.
1395a86bf34SKenneth Heitke  */
1405a86bf34SKenneth Heitke struct spmi_driver {
1415a86bf34SKenneth Heitke 	struct device_driver driver;
1425a86bf34SKenneth Heitke 	int	(*probe)(struct spmi_device *sdev);
1435a86bf34SKenneth Heitke 	void	(*remove)(struct spmi_device *sdev);
144d40c2d4eSHsin-Hsiung Wang 	void	(*shutdown)(struct spmi_device *sdev);
1455a86bf34SKenneth Heitke };
1465a86bf34SKenneth Heitke 
to_spmi_driver(struct device_driver * d)1475a86bf34SKenneth Heitke static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
1485a86bf34SKenneth Heitke {
1495a86bf34SKenneth Heitke 	return container_of(d, struct spmi_driver, driver);
1505a86bf34SKenneth Heitke }
1515a86bf34SKenneth Heitke 
152ff6f4648SStephen Boyd #define spmi_driver_register(sdrv) \
153ff6f4648SStephen Boyd 	__spmi_driver_register(sdrv, THIS_MODULE)
154ff6f4648SStephen Boyd int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner);
1555a86bf34SKenneth Heitke 
1565a86bf34SKenneth Heitke /**
1575a86bf34SKenneth Heitke  * spmi_driver_unregister() - unregister an SPMI client driver
1585a86bf34SKenneth Heitke  * @sdrv:	the driver to unregister
1595a86bf34SKenneth Heitke  */
spmi_driver_unregister(struct spmi_driver * sdrv)1605a86bf34SKenneth Heitke static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
1615a86bf34SKenneth Heitke {
1625a86bf34SKenneth Heitke 	if (sdrv)
1635a86bf34SKenneth Heitke 		driver_unregister(&sdrv->driver);
1645a86bf34SKenneth Heitke }
1655a86bf34SKenneth Heitke 
1665a86bf34SKenneth Heitke #define module_spmi_driver(__spmi_driver) \
1675a86bf34SKenneth Heitke 	module_driver(__spmi_driver, spmi_driver_register, \
1685a86bf34SKenneth Heitke 			spmi_driver_unregister)
1695a86bf34SKenneth Heitke 
1709a2139c2SCaleb Connolly struct device_node;
1719a2139c2SCaleb Connolly 
172272f99edSJohan Hovold struct spmi_device *spmi_find_device_by_of_node(struct device_node *np);
1735a86bf34SKenneth Heitke int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
1745a86bf34SKenneth Heitke int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
1755a86bf34SKenneth Heitke 			   size_t len);
1765a86bf34SKenneth Heitke int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
1775a86bf34SKenneth Heitke 			    size_t len);
1785a86bf34SKenneth Heitke int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
1795a86bf34SKenneth Heitke int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
1805a86bf34SKenneth Heitke int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
1815a86bf34SKenneth Heitke 			    const u8 *buf, size_t len);
1825a86bf34SKenneth Heitke int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
1835a86bf34SKenneth Heitke 			     const u8 *buf, size_t len);
1845a86bf34SKenneth Heitke int spmi_command_reset(struct spmi_device *sdev);
1855a86bf34SKenneth Heitke int spmi_command_sleep(struct spmi_device *sdev);
1865a86bf34SKenneth Heitke int spmi_command_wakeup(struct spmi_device *sdev);
1875a86bf34SKenneth Heitke int spmi_command_shutdown(struct spmi_device *sdev);
1885a86bf34SKenneth Heitke 
1895a86bf34SKenneth Heitke #endif
190