1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun #ifndef __PMAC_PFUNC_H__ 3*4882a593Smuzhiyun #define __PMAC_PFUNC_H__ 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun #include <linux/types.h> 6*4882a593Smuzhiyun #include <linux/list.h> 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun /* Flags in command lists */ 9*4882a593Smuzhiyun #define PMF_FLAGS_ON_INIT 0x80000000u 10*4882a593Smuzhiyun #define PMF_FLGAS_ON_TERM 0x40000000u 11*4882a593Smuzhiyun #define PMF_FLAGS_ON_SLEEP 0x20000000u 12*4882a593Smuzhiyun #define PMF_FLAGS_ON_WAKE 0x10000000u 13*4882a593Smuzhiyun #define PMF_FLAGS_ON_DEMAND 0x08000000u 14*4882a593Smuzhiyun #define PMF_FLAGS_INT_GEN 0x04000000u 15*4882a593Smuzhiyun #define PMF_FLAGS_HIGH_SPEED 0x02000000u 16*4882a593Smuzhiyun #define PMF_FLAGS_LOW_SPEED 0x01000000u 17*4882a593Smuzhiyun #define PMF_FLAGS_SIDE_EFFECTS 0x00800000u 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun /* 20*4882a593Smuzhiyun * Arguments to a platform function call. 21*4882a593Smuzhiyun * 22*4882a593Smuzhiyun * NOTE: By convention, pointer arguments point to an u32 23*4882a593Smuzhiyun */ 24*4882a593Smuzhiyun struct pmf_args { 25*4882a593Smuzhiyun union { 26*4882a593Smuzhiyun u32 v; 27*4882a593Smuzhiyun u32 *p; 28*4882a593Smuzhiyun } u[4]; 29*4882a593Smuzhiyun unsigned int count; 30*4882a593Smuzhiyun }; 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun /* 33*4882a593Smuzhiyun * A driver capable of interpreting commands provides a handlers 34*4882a593Smuzhiyun * structure filled with whatever handlers are implemented by this 35*4882a593Smuzhiyun * driver. Non implemented handlers are left NULL. 36*4882a593Smuzhiyun * 37*4882a593Smuzhiyun * PMF_STD_ARGS are the same arguments that are passed to the parser 38*4882a593Smuzhiyun * and that gets passed back to the various handlers. 39*4882a593Smuzhiyun * 40*4882a593Smuzhiyun * Interpreting a given function always start with a begin() call which 41*4882a593Smuzhiyun * returns an instance data to be passed around subsequent calls, and 42*4882a593Smuzhiyun * ends with an end() call. This allows the low level driver to implement 43*4882a593Smuzhiyun * locking policy or per-function instance data. 44*4882a593Smuzhiyun * 45*4882a593Smuzhiyun * For interrupt capable functions, irq_enable() is called when a client 46*4882a593Smuzhiyun * registers, and irq_disable() is called when the last client unregisters 47*4882a593Smuzhiyun * Note that irq_enable & irq_disable are called within a semaphore held 48*4882a593Smuzhiyun * by the core, thus you should not try to register yourself to some other 49*4882a593Smuzhiyun * pmf interrupt during those calls. 50*4882a593Smuzhiyun */ 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun #define PMF_STD_ARGS struct pmf_function *func, void *instdata, \ 53*4882a593Smuzhiyun struct pmf_args *args 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun struct pmf_function; 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun struct pmf_handlers { 58*4882a593Smuzhiyun void * (*begin)(struct pmf_function *func, struct pmf_args *args); 59*4882a593Smuzhiyun void (*end)(struct pmf_function *func, void *instdata); 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun int (*irq_enable)(struct pmf_function *func); 62*4882a593Smuzhiyun int (*irq_disable)(struct pmf_function *func); 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun int (*write_gpio)(PMF_STD_ARGS, u8 value, u8 mask); 65*4882a593Smuzhiyun int (*read_gpio)(PMF_STD_ARGS, u8 mask, int rshift, u8 xor); 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun int (*write_reg32)(PMF_STD_ARGS, u32 offset, u32 value, u32 mask); 68*4882a593Smuzhiyun int (*read_reg32)(PMF_STD_ARGS, u32 offset); 69*4882a593Smuzhiyun int (*write_reg16)(PMF_STD_ARGS, u32 offset, u16 value, u16 mask); 70*4882a593Smuzhiyun int (*read_reg16)(PMF_STD_ARGS, u32 offset); 71*4882a593Smuzhiyun int (*write_reg8)(PMF_STD_ARGS, u32 offset, u8 value, u8 mask); 72*4882a593Smuzhiyun int (*read_reg8)(PMF_STD_ARGS, u32 offset); 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun int (*delay)(PMF_STD_ARGS, u32 duration); 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun int (*wait_reg32)(PMF_STD_ARGS, u32 offset, u32 value, u32 mask); 77*4882a593Smuzhiyun int (*wait_reg16)(PMF_STD_ARGS, u32 offset, u16 value, u16 mask); 78*4882a593Smuzhiyun int (*wait_reg8)(PMF_STD_ARGS, u32 offset, u8 value, u8 mask); 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun int (*read_i2c)(PMF_STD_ARGS, u32 len); 81*4882a593Smuzhiyun int (*write_i2c)(PMF_STD_ARGS, u32 len, const u8 *data); 82*4882a593Smuzhiyun int (*rmw_i2c)(PMF_STD_ARGS, u32 masklen, u32 valuelen, u32 totallen, 83*4882a593Smuzhiyun const u8 *maskdata, const u8 *valuedata); 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun int (*read_cfg)(PMF_STD_ARGS, u32 offset, u32 len); 86*4882a593Smuzhiyun int (*write_cfg)(PMF_STD_ARGS, u32 offset, u32 len, const u8 *data); 87*4882a593Smuzhiyun int (*rmw_cfg)(PMF_STD_ARGS, u32 offset, u32 masklen, u32 valuelen, 88*4882a593Smuzhiyun u32 totallen, const u8 *maskdata, const u8 *valuedata); 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun int (*read_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 len); 91*4882a593Smuzhiyun int (*write_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 len, const u8 *data); 92*4882a593Smuzhiyun int (*set_i2c_mode)(PMF_STD_ARGS, int mode); 93*4882a593Smuzhiyun int (*rmw_i2c_sub)(PMF_STD_ARGS, u8 subaddr, u32 masklen, u32 valuelen, 94*4882a593Smuzhiyun u32 totallen, const u8 *maskdata, 95*4882a593Smuzhiyun const u8 *valuedata); 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun int (*read_reg32_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift, 98*4882a593Smuzhiyun u32 xor); 99*4882a593Smuzhiyun int (*read_reg16_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift, 100*4882a593Smuzhiyun u32 xor); 101*4882a593Smuzhiyun int (*read_reg8_msrx)(PMF_STD_ARGS, u32 offset, u32 mask, u32 shift, 102*4882a593Smuzhiyun u32 xor); 103*4882a593Smuzhiyun 104*4882a593Smuzhiyun int (*write_reg32_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask); 105*4882a593Smuzhiyun int (*write_reg16_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask); 106*4882a593Smuzhiyun int (*write_reg8_slm)(PMF_STD_ARGS, u32 offset, u32 shift, u32 mask); 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun int (*mask_and_compare)(PMF_STD_ARGS, u32 len, const u8 *maskdata, 109*4882a593Smuzhiyun const u8 *valuedata); 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun struct module *owner; 112*4882a593Smuzhiyun }; 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun /* 116*4882a593Smuzhiyun * Drivers who expose platform functions register at init time, this 117*4882a593Smuzhiyun * causes the platform functions for that device node to be parsed in 118*4882a593Smuzhiyun * advance and associated with the device. The data structures are 119*4882a593Smuzhiyun * partially public so a driver can walk the list of platform functions 120*4882a593Smuzhiyun * and eventually inspect the flags 121*4882a593Smuzhiyun */ 122*4882a593Smuzhiyun struct pmf_device; 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun struct pmf_function { 125*4882a593Smuzhiyun /* All functions for a given driver are linked */ 126*4882a593Smuzhiyun struct list_head link; 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun /* Function node & driver data */ 129*4882a593Smuzhiyun struct device_node *node; 130*4882a593Smuzhiyun void *driver_data; 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun /* For internal use by core */ 133*4882a593Smuzhiyun struct pmf_device *dev; 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun /* The name is the "xxx" in "platform-do-xxx", this is how 136*4882a593Smuzhiyun * platform functions are identified by this code. Some functions 137*4882a593Smuzhiyun * only operate for a given target, in which case the phandle is 138*4882a593Smuzhiyun * here (or 0 if the filter doesn't apply) 139*4882a593Smuzhiyun */ 140*4882a593Smuzhiyun const char *name; 141*4882a593Smuzhiyun u32 phandle; 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun /* The flags for that function. You can have several functions 144*4882a593Smuzhiyun * with the same name and different flag 145*4882a593Smuzhiyun */ 146*4882a593Smuzhiyun u32 flags; 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun /* The actual tokenized function blob */ 149*4882a593Smuzhiyun const void *data; 150*4882a593Smuzhiyun unsigned int length; 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun /* Interrupt clients */ 153*4882a593Smuzhiyun struct list_head irq_clients; 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun /* Refcounting */ 156*4882a593Smuzhiyun struct kref ref; 157*4882a593Smuzhiyun }; 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun /* 160*4882a593Smuzhiyun * For platform functions that are interrupts, one can register 161*4882a593Smuzhiyun * irq_client structures. You canNOT use the same structure twice 162*4882a593Smuzhiyun * as it contains a link member. Also, the callback is called with 163*4882a593Smuzhiyun * a spinlock held, you must not call back into any of the pmf_* functions 164*4882a593Smuzhiyun * from within that callback 165*4882a593Smuzhiyun */ 166*4882a593Smuzhiyun struct pmf_irq_client { 167*4882a593Smuzhiyun void (*handler)(void *data); 168*4882a593Smuzhiyun void *data; 169*4882a593Smuzhiyun struct module *owner; 170*4882a593Smuzhiyun struct list_head link; 171*4882a593Smuzhiyun struct pmf_function *func; 172*4882a593Smuzhiyun }; 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* 176*4882a593Smuzhiyun * Register/Unregister a function-capable driver and its handlers 177*4882a593Smuzhiyun */ 178*4882a593Smuzhiyun extern int pmf_register_driver(struct device_node *np, 179*4882a593Smuzhiyun struct pmf_handlers *handlers, 180*4882a593Smuzhiyun void *driverdata); 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun extern void pmf_unregister_driver(struct device_node *np); 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun 185*4882a593Smuzhiyun /* 186*4882a593Smuzhiyun * Register/Unregister interrupt clients 187*4882a593Smuzhiyun */ 188*4882a593Smuzhiyun extern int pmf_register_irq_client(struct device_node *np, 189*4882a593Smuzhiyun const char *name, 190*4882a593Smuzhiyun struct pmf_irq_client *client); 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun extern void pmf_unregister_irq_client(struct pmf_irq_client *client); 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /* 195*4882a593Smuzhiyun * Called by the handlers when an irq happens 196*4882a593Smuzhiyun */ 197*4882a593Smuzhiyun extern void pmf_do_irq(struct pmf_function *func); 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun /* 201*4882a593Smuzhiyun * Low level call to platform functions. 202*4882a593Smuzhiyun * 203*4882a593Smuzhiyun * The phandle can filter on the target object for functions that have 204*4882a593Smuzhiyun * multiple targets, the flags allow you to restrict the call to a given 205*4882a593Smuzhiyun * combination of flags. 206*4882a593Smuzhiyun * 207*4882a593Smuzhiyun * The args array contains as many arguments as is required by the function, 208*4882a593Smuzhiyun * this is dependent on the function you are calling, unfortunately Apple 209*4882a593Smuzhiyun * mechanism provides no way to encode that so you have to get it right at 210*4882a593Smuzhiyun * the call site. Some functions require no args, in which case, you can 211*4882a593Smuzhiyun * pass NULL. 212*4882a593Smuzhiyun * 213*4882a593Smuzhiyun * You can also pass NULL to the name. This will match any function that has 214*4882a593Smuzhiyun * the appropriate combination of flags & phandle or you can pass 0 to the 215*4882a593Smuzhiyun * phandle to match any 216*4882a593Smuzhiyun */ 217*4882a593Smuzhiyun extern int pmf_do_functions(struct device_node *np, const char *name, 218*4882a593Smuzhiyun u32 phandle, u32 flags, struct pmf_args *args); 219*4882a593Smuzhiyun 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun /* 223*4882a593Smuzhiyun * High level call to a platform function. 224*4882a593Smuzhiyun * 225*4882a593Smuzhiyun * This one looks for the platform-xxx first so you should call it to the 226*4882a593Smuzhiyun * actual target if any. It will fallback to platform-do-xxx if it can't 227*4882a593Smuzhiyun * find one. It will also exclusively target functions that have 228*4882a593Smuzhiyun * the "OnDemand" flag. 229*4882a593Smuzhiyun */ 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun extern int pmf_call_function(struct device_node *target, const char *name, 232*4882a593Smuzhiyun struct pmf_args *args); 233*4882a593Smuzhiyun 234*4882a593Smuzhiyun 235*4882a593Smuzhiyun /* 236*4882a593Smuzhiyun * For low latency interrupt usage, you can lookup for on-demand functions 237*4882a593Smuzhiyun * using the functions below 238*4882a593Smuzhiyun */ 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun extern struct pmf_function *pmf_find_function(struct device_node *target, 241*4882a593Smuzhiyun const char *name); 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun extern struct pmf_function * pmf_get_function(struct pmf_function *func); 244*4882a593Smuzhiyun extern void pmf_put_function(struct pmf_function *func); 245*4882a593Smuzhiyun 246*4882a593Smuzhiyun extern int pmf_call_one(struct pmf_function *func, struct pmf_args *args); 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun int pmac_pfunc_base_install(void); 249*4882a593Smuzhiyun 250*4882a593Smuzhiyun /* Suspend/resume code called by via-pmu directly for now */ 251*4882a593Smuzhiyun extern void pmac_pfunc_base_suspend(void); 252*4882a593Smuzhiyun extern void pmac_pfunc_base_resume(void); 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun #endif /* __PMAC_PFUNC_H__ */ 255