df6f526e2e41e6b2502ad699a758b2621c2637ba
[openwrt/staging/linusw.git] /
1 From 21dace2c687d45819cb0dfc4f32f005da82d9197 Mon Sep 17 00:00:00 2001
2 From: gtrainavicius <gtrainavicius@users.noreply.github.com>
3 Date: Tue, 28 Jan 2020 14:16:37 +0200
4 Subject: [PATCH] pisound: Added reading Pisound board hardware
5 revision and exposing it (#3425)
6
7 pisound: Added reading Pisound board hardware revision and exposing it in kernel log and sysfs file:
8
9 /sys/kernel/pisound/hw_version
10
11 Signed-off-by: Giedrius <giedrius@blokas.io>
12 ---
13 sound/soc/bcm/pisound.c | 86 ++++++++++++++++++++++++++++-------------
14 1 file changed, 59 insertions(+), 27 deletions(-)
15
16 --- a/sound/soc/bcm/pisound.c
17 +++ b/sound/soc/bcm/pisound.c
18 @@ -51,7 +51,8 @@ static void pisnd_spi_set_callback(pisnd
19
20 static const char *pisnd_spi_get_serial(void);
21 static const char *pisnd_spi_get_id(void);
22 -static const char *pisnd_spi_get_version(void);
23 +static const char *pisnd_spi_get_fw_version(void);
24 +static const char *pisnd_spi_get_hw_version(void);
25
26 static int pisnd_midi_init(struct snd_card *card);
27 static void pisnd_midi_uninit(void);
28 @@ -222,7 +223,9 @@ static pisnd_spi_recv_cb g_recvCallback;
29
30 static char g_serial_num[11];
31 static char g_id[25];
32 -static char g_version[5];
33 +enum { MAX_VERSION_STR_LEN = 6 };
34 +static char g_fw_version[MAX_VERSION_STR_LEN];
35 +static char g_hw_version[MAX_VERSION_STR_LEN];
36
37 static uint8_t g_ledFlashDuration;
38 static bool g_ledFlashDurationChanged;
39 @@ -558,7 +561,8 @@ static int spi_read_info(void)
40 char *p;
41
42 memset(g_serial_num, 0, sizeof(g_serial_num));
43 - memset(g_version, 0, sizeof(g_version));
44 + memset(g_fw_version, 0, sizeof(g_fw_version));
45 + strcpy(g_hw_version, "1.0"); // Assume 1.0 hw version.
46 memset(g_id, 0, sizeof(g_id));
47
48 tmp = spi_transfer16(0);
49 @@ -581,12 +585,28 @@ static int spi_read_info(void)
50 return -EINVAL;
51
52 snprintf(
53 - g_version,
54 - sizeof(g_version),
55 + g_fw_version,
56 + MAX_VERSION_STR_LEN,
57 "%x.%02x",
58 buffer[0],
59 buffer[1]
60 );
61 +
62 + g_fw_version[MAX_VERSION_STR_LEN-1] = '\0';
63 + break;
64 + case 3:
65 + if (n != 2)
66 + return -EINVAL;
67 +
68 + snprintf(
69 + g_hw_version,
70 + MAX_VERSION_STR_LEN,
71 + "%x.%x",
72 + buffer[0],
73 + buffer[1]
74 + );
75 +
76 + g_hw_version[MAX_VERSION_STR_LEN-1] = '\0';
77 break;
78 case 1:
79 if (n >= sizeof(g_serial_num))
80 @@ -596,12 +616,14 @@ static int spi_read_info(void)
81 break;
82 case 2:
83 {
84 - if (n >= sizeof(g_id))
85 + if (n*2 >= sizeof(g_id))
86 return -EINVAL;
87
88 p = g_id;
89 for (j = 0; j < n; ++j)
90 p += sprintf(p, "%02x", buffer[j]);
91 +
92 + *p = '\0';
93 }
94 break;
95 default:
96 @@ -619,7 +641,8 @@ static int pisnd_spi_init(struct device
97
98 memset(g_serial_num, 0, sizeof(g_serial_num));
99 memset(g_id, 0, sizeof(g_id));
100 - memset(g_version, 0, sizeof(g_version));
101 + memset(g_fw_version, 0, sizeof(g_fw_version));
102 + memset(g_hw_version, 0, sizeof(g_hw_version));
103
104 spi = pisnd_spi_find_device();
105
106 @@ -729,26 +752,22 @@ static void pisnd_spi_set_callback(pisnd
107
108 static const char *pisnd_spi_get_serial(void)
109 {
110 - if (strlen(g_serial_num))
111 - return g_serial_num;
112 -
113 - return "";
114 + return g_serial_num;
115 }
116
117 static const char *pisnd_spi_get_id(void)
118 {
119 - if (strlen(g_id))
120 - return g_id;
121 -
122 - return "";
123 + return g_id;
124 }
125
126 -static const char *pisnd_spi_get_version(void)
127 +static const char *pisnd_spi_get_fw_version(void)
128 {
129 - if (strlen(g_version))
130 - return g_version;
131 + return g_fw_version;
132 +}
133
134 - return "";
135 +static const char *pisnd_spi_get_hw_version(void)
136 +{
137 + return g_hw_version;
138 }
139
140 static const struct of_device_id pisound_of_match[] = {
141 @@ -1056,13 +1075,22 @@ static ssize_t pisnd_id_show(
142 return sprintf(buf, "%s\n", pisnd_spi_get_id());
143 }
144
145 -static ssize_t pisnd_version_show(
146 +static ssize_t pisnd_fw_version_show(
147 struct kobject *kobj,
148 struct kobj_attribute *attr,
149 char *buf
150 )
151 {
152 - return sprintf(buf, "%s\n", pisnd_spi_get_version());
153 + return sprintf(buf, "%s\n", pisnd_spi_get_fw_version());
154 +}
155 +
156 +static ssize_t pisnd_hw_version_show(
157 + struct kobject *kobj,
158 + struct kobj_attribute *attr,
159 + char *buf
160 +)
161 +{
162 + return sprintf(buf, "%s\n", pisnd_spi_get_hw_version());
163 }
164
165 static ssize_t pisnd_led_store(
166 @@ -1087,15 +1115,18 @@ static struct kobj_attribute pisnd_seria
167 __ATTR(serial, 0444, pisnd_serial_show, NULL);
168 static struct kobj_attribute pisnd_id_attribute =
169 __ATTR(id, 0444, pisnd_id_show, NULL);
170 -static struct kobj_attribute pisnd_version_attribute =
171 - __ATTR(version, 0444, pisnd_version_show, NULL);
172 +static struct kobj_attribute pisnd_fw_version_attribute =
173 + __ATTR(version, 0444, pisnd_fw_version_show, NULL);
174 +static struct kobj_attribute pisnd_hw_version_attribute =
175 +__ATTR(hw_version, 0444, pisnd_hw_version_show, NULL);
176 static struct kobj_attribute pisnd_led_attribute =
177 __ATTR(led, 0644, NULL, pisnd_led_store);
178
179 static struct attribute *attrs[] = {
180 &pisnd_serial_attribute.attr,
181 &pisnd_id_attribute.attr,
182 - &pisnd_version_attribute.attr,
183 + &pisnd_fw_version_attribute.attr,
184 + &pisnd_hw_version_attribute.attr,
185 &pisnd_led_attribute.attr,
186 NULL
187 };
188 @@ -1114,9 +1145,10 @@ static int pisnd_probe(struct platform_d
189 }
190
191 printi("Detected Pisound card:\n");
192 - printi("\tSerial: %s\n", pisnd_spi_get_serial());
193 - printi("\tVersion: %s\n", pisnd_spi_get_version());
194 - printi("\tId: %s\n", pisnd_spi_get_id());
195 + printi("\tSerial: %s\n", pisnd_spi_get_serial());
196 + printi("\tFirmware Version: %s\n", pisnd_spi_get_fw_version());
197 + printi("\tHardware Version: %s\n", pisnd_spi_get_hw_version());
198 + printi("\tId: %s\n", pisnd_spi_get_id());
199
200 pisnd_kobj = kobject_create_and_add("pisound", kernel_kobj);
201 if (!pisnd_kobj) {