1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/platform_device.h>
22 #include <linux/of.h>
23 #include "securec.h"
24
osal_platform_get_modparam_uint(void * pdev,const char * name,unsigned int * value)25 int osal_platform_get_modparam_uint(void *pdev, const char *name, unsigned int *value)
26 {
27 struct device_node *np = ((struct platform_device*)pdev)->dev.of_node;
28 unsigned int temp_value;
29
30 if (of_property_read_u32(np, name, &temp_value)) {
31 return -1;
32 }
33
34 *value = temp_value;
35 return 0;
36 }
37 EXPORT_SYMBOL(osal_platform_get_modparam_uint);
38
osal_platform_get_modparam_int(void * pdev,const char * name,int * value)39 int osal_platform_get_modparam_int(void *pdev, const char *name, int *value)
40 {
41 struct device_node *np = ((struct platform_device*)pdev)->dev.of_node;
42 unsigned int temp_value;
43
44 if (of_property_read_u32(np, name, &temp_value)) {
45 return -1;
46 }
47
48 *value = (int)temp_value;
49 return 0;
50 }
51 EXPORT_SYMBOL(osal_platform_get_modparam_int);
52
osal_platform_get_modparam_uchar(void * pdev,const char * name,unsigned char * value)53 int osal_platform_get_modparam_uchar(void *pdev, const char *name, unsigned char *value)
54 {
55 struct device_node *np = ((struct platform_device*)pdev)->dev.of_node;
56 unsigned char temp_value;
57
58 if (of_property_read_u8(np, name, &temp_value)) {
59 return -1;
60 }
61
62 *value = temp_value;
63 return 0;
64 }
65 EXPORT_SYMBOL(osal_platform_get_modparam_uchar);
66
osal_platform_get_modparam_ushort(void * pdev,const char * name,unsigned short * value)67 int osal_platform_get_modparam_ushort(void *pdev, const char *name, unsigned short *value)
68 {
69 struct device_node *np = ((struct platform_device*)pdev)->dev.of_node;
70 unsigned short temp_value;
71
72 if (of_property_read_u16(np, name, &temp_value)) {
73 return -1;
74 }
75
76 *value = temp_value;
77 return 0;
78 }
79 EXPORT_SYMBOL(osal_platform_get_modparam_ushort);
80
osal_platform_get_modparam_string(void * pdev,const char * name,unsigned int size,char * value)81 int osal_platform_get_modparam_string(void *pdev, const char *name, unsigned int size, char *value)
82 {
83 struct device_node *np = ((struct platform_device*)pdev)->dev.of_node;
84 const char *string = NULL;
85
86 if (of_property_read_string(np, name, &string)) {
87 return -1;
88 }
89
90 if (strncpy_s(value, size, string, size - 1) != EOK) {
91 return -1;
92 }
93 return 0;
94 }
95 EXPORT_SYMBOL(osal_platform_get_modparam_string);
96
osal_of_property_read_u32(const void * np,const char * propname,unsigned int * value)97 int osal_of_property_read_u32(const void *np, const char *propname, unsigned int *value)
98 {
99 return of_property_read_u32((struct device_node*)np, propname, value);
100 }
101 EXPORT_SYMBOL(osal_of_property_read_u32);
102
osal_platform_driver_register(void * drv)103 int osal_platform_driver_register(void *drv)
104 {
105 return __platform_driver_register((struct platform_driver *)drv, THIS_MODULE);
106 }
107 EXPORT_SYMBOL(osal_platform_driver_register);
108
osal_platform_driver_unregister(void * drv)109 void osal_platform_driver_unregister(void *drv)
110 {
111 return platform_driver_unregister((struct platform_driver *)drv);
112 }
113 EXPORT_SYMBOL(osal_platform_driver_unregister);
114
osal_platform_get_resource_byname(void * dev,unsigned int type,const char * name)115 void *osal_platform_get_resource_byname(void *dev, unsigned int type,
116 const char *name)
117 {
118 return (void *)platform_get_resource_byname((struct platform_device *)dev, type, name);
119 }
120 EXPORT_SYMBOL(osal_platform_get_resource_byname);
121
osal_platform_get_resource(void * dev,unsigned int type,unsigned int num)122 void *osal_platform_get_resource(void *dev, unsigned int type, unsigned int num)
123 {
124 return (void *)platform_get_resource((struct platform_device *)dev, type, num);
125 }
126 EXPORT_SYMBOL(osal_platform_get_resource);
127
osal_platform_get_irq(void * dev,unsigned int num)128 int osal_platform_get_irq(void *dev, unsigned int num)
129 {
130 return platform_get_irq((struct platform_device *)dev, num);
131 }
132 EXPORT_SYMBOL(osal_platform_get_irq);
133
osal_platform_get_irq_byname(void * dev,const char * name)134 int osal_platform_get_irq_byname(void *dev, const char *name)
135 {
136 return platform_get_irq_byname((struct platform_device *)dev, name);
137 }
138 EXPORT_SYMBOL(osal_platform_get_irq_byname);
139