1 /*
2  * Copyright (c) 2019-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: qal_vbus_dev
22  * QCA abstraction layer (QAL) virtual bus management APIs
23  */
24 
25 #if !defined(__I_QAL_VBUS_DEV_H)
26 #define __I_QAL_VBUS_DEV_H
27 
28 /* Include Files */
29 #include <qdf_types.h>
30 #include "qdf_util.h"
31 #include "qdf_module.h"
32 #include <linux/of_gpio.h>
33 #include <linux/clk.h>
34 #include <linux/platform_device.h>
35 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
36 #include <linux/reset.h>
37 #endif
38 
39 struct qdf_vbus_resource;
40 struct qdf_vbus_rstctl;
41 struct qdf_dev_clk;
42 struct qdf_pfm_hndl;
43 struct qdf_pfm_drv;
44 struct qdf_device_node;
45 typedef enum of_gpio_flags __qdf_of_gpio_flags;
46 /**
47  * __qal_vbus_get_iorsc() - acquire io resource
48  * @devnum: Device Number
49  * @flag: Property bitmap for the io resource
50  * @devname: Device name string
51  *
52  * This function will allocate the io resource for a device
53  *
54  * Return: QDF_STATUS_SUCCESS on success
55  */
56 static inline QDF_STATUS
__qal_vbus_get_iorsc(int devnum,uint32_t flag,char * devname)57 __qal_vbus_get_iorsc(int devnum, uint32_t flag, char *devname)
58 {
59 	int ret;
60 
61 	ret = gpio_request_one(devnum, flag, devname);
62 
63 	return qdf_status_from_os_return(ret);
64 }
65 
66 /**
67  * __qal_vbus_release_iorsc() - release io resource
68  * @devnum: Device Number
69  *
70  * This function will release the io resource attached to a device
71  *
72  * Return: QDF_STATUS_SUCCESS on success
73  */
74 static inline QDF_STATUS
__qal_vbus_release_iorsc(int devnum)75 __qal_vbus_release_iorsc(int devnum)
76 {
77 	gpio_free(devnum);
78 	return QDF_STATUS_SUCCESS;
79 }
80 
81 /**
82  * __qal_vbus_allocate_iorsc() - allocate io resource
83  * @pinnum: pin Number
84  * @label: name of pin
85  *
86  * This function will allocate io resource
87  *
88  * Return: QDF_STATUS_SUCCESS on success
89  */
90 static inline QDF_STATUS
__qal_vbus_allocate_iorsc(unsigned int pinnum,const char * label)91 __qal_vbus_allocate_iorsc(unsigned int pinnum, const char *label)
92 {
93 	int ret;
94 
95 	ret = gpio_request(pinnum, label);
96 
97 	return qdf_status_from_os_return(ret);
98 }
99 
100 /**
101  * __qal_vbus_iorsc_dir_output() - set pin dirction to output
102  * @pin: pin Number
103  * @val: value
104  *
105  * This function set the gpio pin direction to output
106  *
107  * Return: 0 on success, error no on failure
108  */
109 static inline QDF_STATUS
__qal_vbus_iorsc_dir_output(unsigned int pin,int val)110 __qal_vbus_iorsc_dir_output(unsigned int pin, int val)
111 {
112 	int ret;
113 
114 	ret = gpio_direction_output(pin, val);
115 
116 	return qdf_status_from_os_return(ret);
117 }
118 
119 /**
120  * __qal_vbus_iorsc_set_value() - set pin direction
121  * @pin: pin Number
122  * @val: value
123  *
124  * This function set the gpio pin direction based on value
125  *
126  * Return: QDF_STATUS_SUCCESS on success
127  */
128 static inline QDF_STATUS
__qal_vbus_iorsc_set_value(unsigned int pin,int val)129 __qal_vbus_iorsc_set_value(unsigned int pin, int val)
130 {
131 	gpio_set_value(pin, val);
132 	return QDF_STATUS_SUCCESS;
133 }
134 
135 /**
136  * __qal_vbus_iorsc_toirq() - set irq number to gpio
137  * @pin: pin Number
138  *
139  * This function set the irq number to gpio pin
140  *
141  * Return: QDF_STATUS_SUCCESS on success
142  */
143 static inline QDF_STATUS
__qal_vbus_iorsc_toirq(unsigned int pin)144 __qal_vbus_iorsc_toirq(unsigned int pin)
145 {
146 	int ret;
147 
148 	ret = gpio_to_irq(pin);
149 
150 	return qdf_status_from_os_return(ret);
151 }
152 
153 /**
154  * __qal_vbus_request_irq() - set interrupt handler
155  * @irqnum: irq Number
156  * @handler: function handler to be called
157  * @flags: irq flags
158  * @dev_name: device name
159  * @ctx: pointer to device context
160  *
161  * This function set up the handling of the interrupt
162  *
163  * Return: QDF_STATUS_SUCCESS on success, Error code on failure
164  */
165 static inline QDF_STATUS
__qal_vbus_request_irq(unsigned int irqnum,irqreturn_t (* handler)(int irq,void * arg),unsigned long flags,const char * dev_name,void * ctx)166 __qal_vbus_request_irq(unsigned int irqnum,
167 		       irqreturn_t (*handler)(int irq, void *arg),
168 		       unsigned long flags, const char *dev_name, void *ctx)
169 {
170 	int ret;
171 
172 	ret = request_irq(irqnum, handler, flags, dev_name, ctx);
173 	return qdf_status_from_os_return(ret);
174 }
175 
176 /**
177  * __qal_vbus_free_irq() - free irq
178  * @irqnum: irq Number
179  * @ctx: pointer to device context
180  *
181  * This function free the irq number set to gpio pin
182  *
183  * Return: QDF_STATUS_SUCCESS on success
184  */
185 static inline QDF_STATUS
__qal_vbus_free_irq(unsigned int irqnum,void * ctx)186 __qal_vbus_free_irq(unsigned int irqnum, void *ctx)
187 {
188 	free_irq(irqnum, ctx);
189 	return QDF_STATUS_SUCCESS;
190 }
191 
192 /**
193  * __qal_vbus_enable_devclk() - enable device clock
194  * @clk: Device clock
195  *
196  * This function will enable the clock for a device
197  *
198  * Return: QDF_STATUS_SUCCESS on success
199  */
200 static inline QDF_STATUS
__qal_vbus_enable_devclk(struct qdf_dev_clk * clk)201 __qal_vbus_enable_devclk(struct qdf_dev_clk *clk)
202 {
203 	int ret;
204 
205 	ret = clk_prepare_enable((struct clk *)clk);
206 
207 	return qdf_status_from_os_return(ret);
208 }
209 
210 /**
211  * __qal_vbus_disable_devclk() - disable device clock
212  * @clk: Device clock
213  *
214  * This function will disable the clock for a device
215  *
216  * Return: QDF_STATUS_SUCCESS on success
217  */
218 static inline QDF_STATUS
__qal_vbus_disable_devclk(struct qdf_dev_clk * clk)219 __qal_vbus_disable_devclk(struct qdf_dev_clk *clk)
220 {
221 	clk_disable_unprepare((struct clk *)clk);
222 
223 	return QDF_STATUS_SUCCESS;
224 }
225 
226 /**
227  * __qal_vbus_get_dev_rstctl() - get device reset control
228  * @pfhndl: Device handle
229  * @state: Device state information
230  * @rstctl: Device reset control handle
231  *
232  * This function will acquire the control to reset the device
233  *
234  * Return: QDF_STATUS_SUCCESS on success
235  */
236 static inline QDF_STATUS
__qal_vbus_get_dev_rstctl(struct qdf_pfm_hndl * pfhndl,const char * state,struct qdf_vbus_rstctl ** rstctl)237 __qal_vbus_get_dev_rstctl(struct qdf_pfm_hndl *pfhndl, const char *state,
238 			  struct qdf_vbus_rstctl **rstctl)
239 {
240 	struct reset_control *rsctl;
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0)
242 	rsctl = devm_reset_control_get_optional((struct device *)pfhndl, state);
243 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
244 	rsctl = reset_control_get_optional((struct device *)pfhndl, state);
245 #else
246 	rsctl = NULL;
247 #endif
248 	*rstctl = (struct qdf_vbus_rstctl *)rsctl;
249 	return QDF_STATUS_SUCCESS;
250 }
251 
252 /**
253  * __qal_vbus_release_dev_rstctl() - release device reset control
254  * @pfhndl: Device handle
255  * @rstctl: Device reset control handle
256  *
257  * This function will release the control to reset the device
258  *
259  * Return: QDF_STATUS_SUCCESS on success
260  */
261 static inline QDF_STATUS
__qal_vbus_release_dev_rstctl(struct qdf_pfm_hndl * pfhndl,struct qdf_vbus_rstctl * rstctl)262 __qal_vbus_release_dev_rstctl(struct qdf_pfm_hndl *pfhndl,
263 			      struct qdf_vbus_rstctl *rstctl)
264 {
265 	reset_control_put((struct reset_control *)rstctl);
266 	return QDF_STATUS_SUCCESS;
267 }
268 
269 /**
270  * __qal_vbus_activate_dev_rstctl() - activate device reset control
271  * @pfhndl: Device handle
272  * @rstctl: Device reset control handle
273  *
274  * This function will activate the reset control for the device
275  *
276  * Return: QDF_STATUS_SUCCESS on success
277  */
278 static inline QDF_STATUS
__qal_vbus_activate_dev_rstctl(struct qdf_pfm_hndl * pfhndl,struct qdf_vbus_rstctl * rstctl)279 __qal_vbus_activate_dev_rstctl(struct qdf_pfm_hndl *pfhndl,
280 			       struct qdf_vbus_rstctl *rstctl)
281 {
282 	reset_control_assert((struct reset_control *)rstctl);
283 	return QDF_STATUS_SUCCESS;
284 }
285 
286 /**
287  * __qal_vbus_deactivate_dev_rstctl() - deactivate device reset control
288  * @pfhndl: Device handle
289  * @rstctl: Device reset control handle
290  *
291  * This function will deactivate the reset control for the device
292  *
293  * Return: QDF_STATUS_SUCCESS on success
294  */
295 static inline QDF_STATUS
__qal_vbus_deactivate_dev_rstctl(struct qdf_pfm_hndl * pfhndl,struct qdf_vbus_rstctl * rstctl)296 __qal_vbus_deactivate_dev_rstctl(struct qdf_pfm_hndl *pfhndl,
297 				 struct qdf_vbus_rstctl *rstctl)
298 {
299 	reset_control_deassert((struct reset_control *)rstctl);
300 	return QDF_STATUS_SUCCESS;
301 }
302 
303 /**
304  * __qal_vbus_get_resource() - get resource
305  * @pfhndl: Device handle
306  * @rsc: Resource handle
307  * @restype: Resource type
308  * @residx: Resource index
309  *
310  * This function will acquire a particular resource and attach it to the device
311  *
312  * Return: QDF_STATUS_SUCCESS on success
313  */
314 static inline QDF_STATUS
__qal_vbus_get_resource(struct qdf_pfm_hndl * pfhndl,struct qdf_vbus_resource ** rsc,uint32_t restype,uint32_t residx)315 __qal_vbus_get_resource(struct qdf_pfm_hndl *pfhndl,
316 			struct qdf_vbus_resource **rsc, uint32_t restype,
317 			uint32_t residx)
318 {
319 	struct resource *rsrc;
320 
321 	rsrc = platform_get_resource((struct platform_device *)pfhndl,
322 				     restype, residx);
323 	*rsc = (struct qdf_vbus_resource *)rsrc;
324 	return QDF_STATUS_SUCCESS;
325 }
326 
327 /**
328  * __qal_vbus_get_irq() - get irq
329  * @pfhndl: Device handle
330  * @str: Device identifier
331  * @irq: irq number
332  *
333  * This function will acquire an irq for the device
334  *
335  * Return: QDF_STATUS_SUCCESS on success
336  */
337 static inline QDF_STATUS
__qal_vbus_get_irq(struct qdf_pfm_hndl * pfhndl,const char * str,int * irq)338 __qal_vbus_get_irq(struct qdf_pfm_hndl *pfhndl, const char *str, int *irq)
339 {
340 	*irq = platform_get_irq_byname((struct platform_device *)pfhndl, str);
341 
342 	if (*irq < 0)
343 		return QDF_STATUS_E_FAULT;
344 
345 	return QDF_STATUS_SUCCESS;
346 }
347 
348 /**
349  * __qal_vbus_register_driver() - register driver
350  * @pfdev: Device handle
351  *
352  * This function will initialize a device
353  *
354  * Return: QDF_STATUS_SUCCESS on success
355  */
356 static inline QDF_STATUS
__qal_vbus_register_driver(struct qdf_pfm_drv * pfdev)357 __qal_vbus_register_driver(struct qdf_pfm_drv *pfdev)
358 {
359 	int ret;
360 
361 	ret = platform_driver_register((struct platform_driver *)pfdev);
362 
363 	return qdf_status_from_os_return(ret);
364 }
365 
366 /**
367  * __qal_vbus_deregister_driver() - deregister driver
368  * @pfdev: Device handle
369  *
370  * This function will deregister the driver for a device
371  *
372  * Return: QDF_STATUS_SUCCESS on success
373  */
374 static inline QDF_STATUS
__qal_vbus_deregister_driver(struct qdf_pfm_drv * pfdev)375 __qal_vbus_deregister_driver(struct qdf_pfm_drv *pfdev)
376 {
377 	platform_driver_unregister((struct platform_driver *)pfdev);
378 
379 	return QDF_STATUS_SUCCESS;
380 }
381 
382 /**
383  * __qal_vbus_gpio_set_value_cansleep() - assign a gpio's raw value
384  * @gpio: gpio whose value will be assigned
385  * @value: value to assign
386  *
387  * Return: QDF_STATUS_SUCCESS on success
388  */
389 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0)
390 static inline QDF_STATUS
__qal_vbus_gpio_set_value_cansleep(unsigned int gpio,int value)391 __qal_vbus_gpio_set_value_cansleep(unsigned int gpio, int value)
392 {
393 	gpio_set_value_cansleep(gpio, value);
394 
395 	return QDF_STATUS_SUCCESS;
396 }
397 #else
398 static inline QDF_STATUS
__qal_vbus_gpio_set_value_cansleep(unsigned int gpio,int value)399 __qal_vbus_gpio_set_value_cansleep(unsigned int gpio, int value)
400 {
401 	return QDF_STATUS_SUCCESS;
402 }
403 #endif
404 
405 /**
406  * __qal_vbus_rcu_read_lock() - mark the beginning of an RCU read-side critical
407  * section
408  *
409  * Return: QDF_STATUS_SUCCESS on success
410  */
411 static inline QDF_STATUS
__qal_vbus_rcu_read_lock(void)412 __qal_vbus_rcu_read_lock(void)
413 {
414 	rcu_read_lock();
415 
416 	return QDF_STATUS_SUCCESS;
417 }
418 
419 /**
420  * __qal_vbus_rcu_read_unlock() - marks the end of an RCU read-side critical
421  * section.
422  *
423  * Return: QDF_STATUS_SUCCESS on success
424  */
425 static inline QDF_STATUS
__qal_vbus_rcu_read_unlock(void)426 __qal_vbus_rcu_read_unlock(void)
427 {
428 	rcu_read_unlock();
429 
430 	return QDF_STATUS_SUCCESS;
431 }
432 
433 #if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0))
434 /**
435  * __qal_vbus_of_get_named_gpio_flags() - Get a GPIO descriptor and flags
436  * for GPIO API
437  * @np: device node to get GPIO from
438  * @list_name: property name containing gpio specifier(s)
439  * @index: index of the GPIO
440  * @flags: a flags pointer to fill in
441  *
442  * The global GPIO number for the GPIO specified by its descriptor.
443  */
444 static inline int
__qal_vbus_of_get_named_gpio_flags(struct qdf_device_node * np,const char * list_name,int index,__qdf_of_gpio_flags * flags)445 __qal_vbus_of_get_named_gpio_flags(struct qdf_device_node *np,
446 				   const char *list_name,
447 				   int index, __qdf_of_gpio_flags *flags)
448 {
449 	return of_get_named_gpio_flags((struct device_node *)np,
450 				       list_name, index, flags);
451 }
452 #else
453 static inline int
__qal_vbus_of_get_named_gpio_flags(struct qdf_device_node * np,const char * list_name,int index,__qdf_of_gpio_flags * flags)454 __qal_vbus_of_get_named_gpio_flags(struct qdf_device_node *np,
455 				   const char *list_name,
456 				   int index, __qdf_of_gpio_flags *flags)
457 {
458 	QDF_ASSERT(0);
459 	return QDF_STATUS_E_NOSUPPORT;
460 }
461 #endif
462 
463 #endif /* __I_QAL_VBUS_DEV_H */
464