18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __PMAC_PFUNC_H__
38c2ecf20Sopenharmony_ci#define __PMAC_PFUNC_H__
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/types.h>
68c2ecf20Sopenharmony_ci#include <linux/list.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/* Flags in command lists */
98c2ecf20Sopenharmony_ci#define PMF_FLAGS_ON_INIT		0x80000000u
108c2ecf20Sopenharmony_ci#define PMF_FLGAS_ON_TERM		0x40000000u
118c2ecf20Sopenharmony_ci#define PMF_FLAGS_ON_SLEEP		0x20000000u
128c2ecf20Sopenharmony_ci#define PMF_FLAGS_ON_WAKE		0x10000000u
138c2ecf20Sopenharmony_ci#define PMF_FLAGS_ON_DEMAND		0x08000000u
148c2ecf20Sopenharmony_ci#define PMF_FLAGS_INT_GEN		0x04000000u
158c2ecf20Sopenharmony_ci#define PMF_FLAGS_HIGH_SPEED		0x02000000u
168c2ecf20Sopenharmony_ci#define PMF_FLAGS_LOW_SPEED		0x01000000u
178c2ecf20Sopenharmony_ci#define PMF_FLAGS_SIDE_EFFECTS		0x00800000u
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * Arguments to a platform function call.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * NOTE: By convention, pointer arguments point to an u32
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_cistruct pmf_args {
258c2ecf20Sopenharmony_ci	union {
268c2ecf20Sopenharmony_ci		u32 v;
278c2ecf20Sopenharmony_ci		u32 *p;
288c2ecf20Sopenharmony_ci	} u[4];
298c2ecf20Sopenharmony_ci	unsigned int count;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * A driver capable of interpreting commands provides a handlers
348c2ecf20Sopenharmony_ci * structure filled with whatever handlers are implemented by this
358c2ecf20Sopenharmony_ci * driver. Non implemented handlers are left NULL.
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * PMF_STD_ARGS are the same arguments that are passed to the parser
388c2ecf20Sopenharmony_ci * and that gets passed back to the various handlers.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * Interpreting a given function always start with a begin() call which
418c2ecf20Sopenharmony_ci * returns an instance data to be passed around subsequent calls, and
428c2ecf20Sopenharmony_ci * ends with an end() call. This allows the low level driver to implement
438c2ecf20Sopenharmony_ci * locking policy or per-function instance data.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * For interrupt capable functions, irq_enable() is called when a client
468c2ecf20Sopenharmony_ci * registers, and irq_disable() is called when the last client unregisters
478c2ecf20Sopenharmony_ci * Note that irq_enable & irq_disable are called within a semaphore held
488c2ecf20Sopenharmony_ci * by the core, thus you should not try to register yourself to some other
498c2ecf20Sopenharmony_ci * pmf interrupt during those calls.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define PMF_STD_ARGS	struct pmf_function *func, void *instdata, \
538c2ecf20Sopenharmony_ci		        struct pmf_args *args
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct pmf_function;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct pmf_handlers {
588c2ecf20Sopenharmony_ci	void * (*begin)(struct pmf_function *func, struct pmf_args *args);
598c2ecf20Sopenharmony_ci	void (*end)(struct pmf_function *func, void *instdata);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	int (*irq_enable)(struct pmf_function *func);
628c2ecf20Sopenharmony_ci	int (*irq_disable)(struct pmf_function *func);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	int (*write_gpio)(PMF_STD_ARGS, u8 value, u8 mask);
658c2ecf20Sopenharmony_ci	int (*read_gpio)(PMF_STD_ARGS, u8 mask, int rshift, u8 xor);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	int (*write_reg32)(PMF_STD_ARGS, u32 offset, u32 value, u32 mask);
688c2ecf20Sopenharmony_ci	int (*read_reg32)(PMF_STD_ARGS, u32 offset);
698c2ecf20Sopenharmony_ci	int (*write_reg16)(PMF_STD_ARGS, u32 offset, u16 value, u16 mask);
708c2ecf20Sopenharmony_ci	int (*read_reg16)(PMF_STD_ARGS, u32 offset);
718c2ecf20Sopenharmony_ci	int (*write_reg8)(PMF_STD_ARGS, u32 offset, u8 value, u8 mask);
728c2ecf20Sopenharmony_ci	int (*read_reg8)(PMF_STD_ARGS, u32 offset);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	int (*delay)(PMF_STD_ARGS, u32 duration);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	int (*wait_reg32)(PMF_STD_ARGS, u32 offset, u32 value, u32 mask);
778c2ecf20Sopenharmony_ci	int (*wait_reg16)(PMF_STD_ARGS, u32 offset, u16 value, u16 mask);
788c2ecf20Sopenharmony_ci	int (*wait_reg8)(PMF_STD_ARGS, u32 offset, u8 value, u8 mask);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	int (*read_i2c)(PMF_STD_ARGS, u32 len);
818c2ecf20Sopenharmony_ci	int (*write_i2c)(PMF_STD_ARGS, u32 len, const u8 *data);
828c2ecf20Sopenharmony_ci	int (*rmw_i2c)(PMF_STD_ARGS, u32 masklen, u32 valuelen, u32 totallen,
838c2ecf20Sopenharmony_ci		       const u8 *maskdata, const u8 *valuedata);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	int (*read_cfg)(PMF_STD_ARGS, u32 offset, u32 len);
868c2ecf20Sopenharmony_ci	int (*write_cfg)(PMF_STD_ARGS, u32 offset, u32 len, const u8 *data);
878c2ecf20Sopenharmony_ci	int (*rmw_cfg)(PMF_STD_ARGS, u32 offset, u32 masklen, u32 valuelen,
888c2ecf20Sopenharmony_ci		       u32 totallen, const u8 *maskdata, const u8 *valuedata);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	int (*read_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 len);
918c2ecf20Sopenharmony_ci	int (*write_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 len, const u8 *data);
928c2ecf20Sopenharmony_ci	int (*set_i2c_mode)(PMF_STD_ARGS, int mode);
938c2ecf20Sopenharmony_ci	int (*rmw_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 masklen, u32 valuelen,
948c2ecf20Sopenharmony_ci			   u32 totallen, const u8 *maskdata,
958c2ecf20Sopenharmony_ci			   const u8 *valuedata);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	int (*read_reg32_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift,
988c2ecf20Sopenharmony_ci			       u32 xor);
998c2ecf20Sopenharmony_ci	int (*read_reg16_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift,
1008c2ecf20Sopenharmony_ci			       u32 xor);
1018c2ecf20Sopenharmony_ci	int (*read_reg8_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift,
1028c2ecf20Sopenharmony_ci			      u32 xor);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	int (*write_reg32_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask);
1058c2ecf20Sopenharmony_ci	int (*write_reg16_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask);
1068c2ecf20Sopenharmony_ci	int (*write_reg8_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	int (*mask_and_compare)(PMF_STD_ARGS, u32 len, const u8 *maskdata,
1098c2ecf20Sopenharmony_ci				const u8 *valuedata);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	struct module *owner;
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/*
1168c2ecf20Sopenharmony_ci * Drivers who expose platform functions register at init time, this
1178c2ecf20Sopenharmony_ci * causes the platform functions for that device node to be parsed in
1188c2ecf20Sopenharmony_ci * advance and associated with the device. The data structures are
1198c2ecf20Sopenharmony_ci * partially public so a driver can walk the list of platform functions
1208c2ecf20Sopenharmony_ci * and eventually inspect the flags
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cistruct pmf_device;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistruct pmf_function {
1258c2ecf20Sopenharmony_ci	/* All functions for a given driver are linked */
1268c2ecf20Sopenharmony_ci	struct list_head	link;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* Function node & driver data */
1298c2ecf20Sopenharmony_ci	struct device_node	*node;
1308c2ecf20Sopenharmony_ci	void			*driver_data;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/* For internal use by core */
1338c2ecf20Sopenharmony_ci	struct pmf_device	*dev;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* The name is the "xxx" in "platform-do-xxx", this is how
1368c2ecf20Sopenharmony_ci	 * platform functions are identified by this code. Some functions
1378c2ecf20Sopenharmony_ci	 * only operate for a given target, in which case the phandle is
1388c2ecf20Sopenharmony_ci	 * here (or 0 if the filter doesn't apply)
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	const char		*name;
1418c2ecf20Sopenharmony_ci	u32			phandle;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* The flags for that function. You can have several functions
1448c2ecf20Sopenharmony_ci	 * with the same name and different flag
1458c2ecf20Sopenharmony_ci	 */
1468c2ecf20Sopenharmony_ci	u32			flags;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* The actual tokenized function blob */
1498c2ecf20Sopenharmony_ci	const void		*data;
1508c2ecf20Sopenharmony_ci	unsigned int		length;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/* Interrupt clients */
1538c2ecf20Sopenharmony_ci	struct list_head	irq_clients;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Refcounting */
1568c2ecf20Sopenharmony_ci	struct kref		ref;
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/*
1608c2ecf20Sopenharmony_ci * For platform functions that are interrupts, one can register
1618c2ecf20Sopenharmony_ci * irq_client structures. You canNOT use the same structure twice
1628c2ecf20Sopenharmony_ci * as it contains a link member. Also, the callback is called with
1638c2ecf20Sopenharmony_ci * a spinlock held, you must not call back into any of the pmf_* functions
1648c2ecf20Sopenharmony_ci * from within that callback
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_cistruct pmf_irq_client {
1678c2ecf20Sopenharmony_ci	void			(*handler)(void *data);
1688c2ecf20Sopenharmony_ci	void			*data;
1698c2ecf20Sopenharmony_ci	struct module		*owner;
1708c2ecf20Sopenharmony_ci	struct list_head	link;
1718c2ecf20Sopenharmony_ci	struct pmf_function	*func;
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/*
1768c2ecf20Sopenharmony_ci * Register/Unregister a function-capable driver and its handlers
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_ciextern int pmf_register_driver(struct device_node *np,
1798c2ecf20Sopenharmony_ci			      struct pmf_handlers *handlers,
1808c2ecf20Sopenharmony_ci			      void *driverdata);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ciextern void pmf_unregister_driver(struct device_node *np);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/*
1868c2ecf20Sopenharmony_ci * Register/Unregister interrupt clients
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_ciextern int pmf_register_irq_client(struct device_node *np,
1898c2ecf20Sopenharmony_ci				   const char *name,
1908c2ecf20Sopenharmony_ci				   struct pmf_irq_client *client);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ciextern void pmf_unregister_irq_client(struct pmf_irq_client *client);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/*
1958c2ecf20Sopenharmony_ci * Called by the handlers when an irq happens
1968c2ecf20Sopenharmony_ci */
1978c2ecf20Sopenharmony_ciextern void pmf_do_irq(struct pmf_function *func);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/*
2018c2ecf20Sopenharmony_ci * Low level call to platform functions.
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * The phandle can filter on the target object for functions that have
2048c2ecf20Sopenharmony_ci * multiple targets, the flags allow you to restrict the call to a given
2058c2ecf20Sopenharmony_ci * combination of flags.
2068c2ecf20Sopenharmony_ci *
2078c2ecf20Sopenharmony_ci * The args array contains as many arguments as is required by the function,
2088c2ecf20Sopenharmony_ci * this is dependent on the function you are calling, unfortunately Apple
2098c2ecf20Sopenharmony_ci * mechanism provides no way to encode that so you have to get it right at
2108c2ecf20Sopenharmony_ci * the call site. Some functions require no args, in which case, you can
2118c2ecf20Sopenharmony_ci * pass NULL.
2128c2ecf20Sopenharmony_ci *
2138c2ecf20Sopenharmony_ci * You can also pass NULL to the name. This will match any function that has
2148c2ecf20Sopenharmony_ci * the appropriate combination of flags & phandle or you can pass 0 to the
2158c2ecf20Sopenharmony_ci * phandle to match any
2168c2ecf20Sopenharmony_ci */
2178c2ecf20Sopenharmony_ciextern int pmf_do_functions(struct device_node *np, const char *name,
2188c2ecf20Sopenharmony_ci			    u32 phandle, u32 flags, struct pmf_args *args);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/*
2238c2ecf20Sopenharmony_ci * High level call to a platform function.
2248c2ecf20Sopenharmony_ci *
2258c2ecf20Sopenharmony_ci * This one looks for the platform-xxx first so you should call it to the
2268c2ecf20Sopenharmony_ci * actual target if any. It will fallback to platform-do-xxx if it can't
2278c2ecf20Sopenharmony_ci * find one. It will also exclusively target functions that have
2288c2ecf20Sopenharmony_ci * the "OnDemand" flag.
2298c2ecf20Sopenharmony_ci */
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciextern int pmf_call_function(struct device_node *target, const char *name,
2328c2ecf20Sopenharmony_ci			     struct pmf_args *args);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci/*
2368c2ecf20Sopenharmony_ci * For low latency interrupt usage, you can lookup for on-demand functions
2378c2ecf20Sopenharmony_ci * using the functions below
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciextern struct pmf_function *pmf_find_function(struct device_node *target,
2418c2ecf20Sopenharmony_ci					      const char *name);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciextern struct pmf_function * pmf_get_function(struct pmf_function *func);
2448c2ecf20Sopenharmony_ciextern void pmf_put_function(struct pmf_function *func);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ciextern int pmf_call_one(struct pmf_function *func, struct pmf_args *args);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ciint pmac_pfunc_base_install(void);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* Suspend/resume code called by via-pmu directly for now */
2518c2ecf20Sopenharmony_ciextern void pmac_pfunc_base_suspend(void);
2528c2ecf20Sopenharmony_ciextern void pmac_pfunc_base_resume(void);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#endif /* __PMAC_PFUNC_H__ */
255