]>
Commit | Line | Data |
---|---|---|
dfaee1de BG |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
6ec6c43b | 20 | #include <config.h> |
ad4c0275 | 21 | #include <math.h> |
6b80b80d | 22 | #include <string.h> |
391e23a9 | 23 | #include <stdlib.h> |
6b80b80d | 24 | #include <errno.h> |
391e23a9 | 25 | #include <fcntl.h> |
4678bdb6 | 26 | #include <arpa/inet.h> |
61f2b7f7 | 27 | #include <glib/gstdio.h> |
dfaee1de | 28 | #include "protocol.h" |
740ad48a | 29 | #include "gpio.h" |
dfaee1de | 30 | |
4678bdb6 BG |
31 | #define ACME_REV_A 1 |
32 | #define ACME_REV_B 2 | |
33 | ||
133016f6 BG |
34 | enum channel_type { |
35 | ENRG_PWR = 1, | |
36 | ENRG_CURR, | |
37 | ENRG_VOL, | |
38 | TEMP_IN, | |
39 | TEMP_OUT, | |
40 | }; | |
41 | ||
6b80b80d | 42 | struct channel_group_priv { |
4678bdb6 | 43 | uint8_t rev; |
6b80b80d | 44 | int hwmon_num; |
61f2b7f7 | 45 | int probe_type; |
740ad48a | 46 | int index; |
8f196120 | 47 | int has_pws; |
4678bdb6 | 48 | uint32_t pws_gpio; |
6b80b80d BG |
49 | }; |
50 | ||
51 | struct channel_priv { | |
52 | int ch_type; | |
4e88b86c | 53 | int fd; |
ad4c0275 | 54 | int digits; |
7e5a048f | 55 | float val; |
6b80b80d BG |
56 | struct channel_group_priv *probe; |
57 | }; | |
58 | ||
4678bdb6 BG |
59 | #define EEPROM_SERIAL_SIZE 16 |
60 | #define EEPROM_TAG_SIZE 32 | |
61 | ||
62 | #define EEPROM_PROBE_TYPE_USB 1 | |
63 | #define EEPROM_PROBE_TYPE_JACK 2 | |
64 | #define EEPROM_PROBE_TYPE_HE10 3 | |
65 | ||
66 | struct probe_eeprom { | |
453a0c2e BG |
67 | uint32_t type; |
68 | uint32_t rev; | |
69 | uint32_t shunt; | |
4678bdb6 BG |
70 | uint8_t pwr_sw; |
71 | uint8_t serial[EEPROM_SERIAL_SIZE]; | |
453a0c2e | 72 | int8_t tag[EEPROM_TAG_SIZE]; |
4c3a4bca BG |
73 | }; |
74 | ||
75 | #define EEPROM_SIZE (3 * sizeof(uint32_t) + 1 + EEPROM_SERIAL_SIZE + EEPROM_TAG_SIZE) | |
76 | ||
77 | #define EEPROM_OFF_TYPE 0 | |
78 | #define EEPROM_OFF_REV sizeof(uint32_t) | |
79 | #define EEPROM_OFF_SHUNT (2 * sizeof(uint32_t)) | |
80 | #define EEPROM_OFF_PWR_SW (3 * sizeof(uint32_t)) | |
81 | #define EEPROM_OFF_SERIAL (3 * sizeof(uint32_t) + 1) | |
82 | #define EEPROM_OFF_TAG (EEPROM_OFF_SERIAL + EEPROM_SERIAL_SIZE) | |
4678bdb6 | 83 | |
6b80b80d BG |
84 | static const uint8_t enrg_i2c_addrs[] = { |
85 | 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47, | |
86 | }; | |
87 | ||
88 | static const uint8_t temp_i2c_addrs[] = { | |
89 | 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b, | |
90 | }; | |
91 | ||
4678bdb6 | 92 | static const uint32_t revA_pws_gpios[] = { |
391e23a9 | 93 | 486, 498, 502, 482, 478, 506, 510, 474, |
740ad48a BG |
94 | }; |
95 | ||
4678bdb6 | 96 | static const uint32_t revA_pws_info_gpios[] = { |
740ad48a BG |
97 | 487, 499, 503, 483, 479, 507, 511, 475, |
98 | }; | |
99 | ||
4678bdb6 BG |
100 | static const uint32_t revB_pws_gpios[] = { |
101 | 489, 491, 493, 495, 497, 499, 501, 503, | |
102 | }; | |
103 | ||
61f2b7f7 BG |
104 | #define MOHM_TO_UOHM(x) ((x) * 1000) |
105 | #define UOHM_TO_MOHM(x) ((x) / 1000) | |
106 | ||
6b80b80d BG |
107 | SR_PRIV uint8_t bl_acme_get_enrg_addr(int index) |
108 | { | |
109 | return enrg_i2c_addrs[index]; | |
110 | } | |
111 | ||
112 | SR_PRIV uint8_t bl_acme_get_temp_addr(int index) | |
113 | { | |
114 | return temp_i2c_addrs[index]; | |
115 | } | |
116 | ||
117 | SR_PRIV gboolean bl_acme_is_sane(void) | |
118 | { | |
119 | gboolean status; | |
120 | ||
121 | /* | |
122 | * We expect sysfs to be present and mounted at /sys, ina226 and | |
123 | * tmp435 sensors detected by the system and their appropriate | |
124 | * drivers loaded and functional. | |
125 | */ | |
126 | status = g_file_test("/sys", G_FILE_TEST_IS_DIR); | |
127 | if (!status) { | |
128 | sr_err("/sys/ directory not found - sysfs not mounted?"); | |
129 | return FALSE; | |
130 | } | |
131 | ||
132 | return TRUE; | |
133 | } | |
134 | ||
135 | static void probe_name_path(unsigned int addr, GString *path) | |
136 | { | |
137 | g_string_printf(path, | |
138 | "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr); | |
139 | } | |
140 | ||
141 | /* | |
142 | * For given address fill buf with the path to appropriate hwmon entry. | |
143 | */ | |
144 | static void probe_hwmon_path(unsigned int addr, GString *path) | |
145 | { | |
146 | g_string_printf(path, | |
147 | "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr); | |
148 | } | |
149 | ||
4678bdb6 BG |
150 | static void probe_eeprom_path(unsigned int addr, GString *path) |
151 | { | |
152 | g_string_printf(path, | |
153 | "/sys/class/i2c-dev/i2c-1/device/1-00%02x/eeprom", | |
154 | addr + 0x10); | |
155 | } | |
156 | ||
6b80b80d BG |
157 | SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr, |
158 | int prb_num, const char *prb_name) | |
159 | { | |
160 | gboolean ret = FALSE, status; | |
161 | char *buf = NULL; | |
162 | GString *path = g_string_sized_new(64); | |
163 | GError *err = NULL; | |
164 | gsize size; | |
165 | ||
166 | probe_name_path(addr, path); | |
167 | status = g_file_get_contents(path->str, &buf, &size, &err); | |
168 | if (!status) { | |
169 | sr_dbg("Name for probe %d can't be read: %s", | |
170 | prb_num, err->message); | |
171 | g_string_free(path, TRUE); | |
a64fec2b | 172 | g_error_free(err); |
6b80b80d | 173 | return ret; |
6b80b80d BG |
174 | } |
175 | ||
391e23a9 | 176 | if (!strncmp(buf, prb_name, strlen(prb_name))) { |
6b80b80d BG |
177 | /* |
178 | * Correct driver registered on this address - but is | |
179 | * there an actual probe connected? | |
180 | */ | |
181 | probe_hwmon_path(addr, path); | |
182 | status = g_file_test(path->str, G_FILE_TEST_IS_DIR); | |
183 | if (status) { | |
184 | /* We have found an ACME probe. */ | |
185 | ret = TRUE; | |
186 | } | |
187 | } | |
188 | ||
189 | g_free(buf); | |
190 | g_string_free(path, TRUE); | |
191 | ||
192 | return ret; | |
193 | } | |
194 | ||
195 | static int get_hwmon_index(unsigned int addr) | |
196 | { | |
197 | int status, hwmon; | |
198 | GString *path = g_string_sized_new(64); | |
199 | GError *err = NULL; | |
200 | GDir *dir; | |
201 | ||
202 | probe_hwmon_path(addr, path); | |
203 | dir = g_dir_open(path->str, 0, &err); | |
391e23a9 | 204 | if (!dir) { |
6b80b80d BG |
205 | sr_err("Error opening %s: %s", path->str, err->message); |
206 | g_string_free(path, TRUE); | |
a64fec2b | 207 | g_error_free(err); |
6b80b80d BG |
208 | return -1; |
209 | } | |
210 | ||
211 | g_string_free(path, TRUE); | |
212 | ||
213 | /* | |
214 | * The directory should contain a single file named hwmonX where X | |
215 | * is the hwmon index. | |
216 | */ | |
217 | status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon); | |
218 | g_dir_close(dir); | |
219 | if (status != 1) { | |
220 | sr_err("Unable to determine the hwmon entry"); | |
221 | return -1; | |
222 | } | |
223 | ||
224 | return hwmon; | |
225 | } | |
226 | ||
391e23a9 | 227 | static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg, |
6b80b80d BG |
228 | int index, int type) |
229 | { | |
230 | struct channel_priv *cp; | |
231 | struct dev_context *devc; | |
232 | struct sr_channel *ch; | |
233 | char *name; | |
234 | ||
235 | devc = sdi->priv; | |
236 | ||
237 | switch (type) { | |
238 | case ENRG_PWR: | |
239 | name = g_strdup_printf("P%d_ENRG_PWR", index); | |
240 | break; | |
241 | case ENRG_CURR: | |
242 | name = g_strdup_printf("P%d_ENRG_CURR", index); | |
243 | break; | |
244 | case ENRG_VOL: | |
245 | name = g_strdup_printf("P%d_ENRG_VOL", index); | |
246 | break; | |
247 | case TEMP_IN: | |
248 | name = g_strdup_printf("P%d_TEMP_IN", index); | |
249 | break; | |
250 | case TEMP_OUT: | |
251 | name = g_strdup_printf("P%d_TEMP_OUT", index); | |
252 | break; | |
253 | default: | |
391e23a9 | 254 | sr_err("Invalid channel type: %d.", type); |
6b80b80d BG |
255 | return; |
256 | } | |
257 | ||
258 | cp = g_malloc0(sizeof(struct channel_priv)); | |
259 | cp->ch_type = type; | |
260 | cp->probe = cg->priv; | |
261 | ||
5e23fcab | 262 | ch = sr_channel_new(sdi, devc->num_channels++, |
6b80b80d BG |
263 | SR_CHANNEL_ANALOG, TRUE, name); |
264 | g_free(name); | |
265 | ||
266 | ch->priv = cp; | |
267 | cg->channels = g_slist_append(cg->channels, ch); | |
6b80b80d BG |
268 | } |
269 | ||
4678bdb6 BG |
270 | static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom) |
271 | { | |
4678bdb6 | 272 | GString *path = g_string_sized_new(64); |
4c3a4bca | 273 | char eeprom_buf[EEPROM_SIZE]; |
4678bdb6 BG |
274 | ssize_t rd; |
275 | int fd; | |
276 | ||
277 | probe_eeprom_path(addr, path); | |
278 | fd = g_open(path->str, O_RDONLY); | |
279 | g_string_free(path, TRUE); | |
280 | if (fd < 0) | |
281 | return -1; | |
282 | ||
4c3a4bca | 283 | rd = read(fd, eeprom_buf, EEPROM_SIZE); |
e4388768 | 284 | close(fd); |
4c3a4bca | 285 | if (rd != EEPROM_SIZE) |
4678bdb6 BG |
286 | return -1; |
287 | ||
a7da85f5 BG |
288 | eeprom->type = RB32(eeprom_buf + EEPROM_OFF_TYPE); |
289 | eeprom->rev = RB32(eeprom_buf + EEPROM_OFF_REV); | |
290 | eeprom->shunt = RB32(eeprom_buf + EEPROM_OFF_SHUNT); | |
4c3a4bca BG |
291 | eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW); |
292 | /* Don't care about the serial number and tag for now. */ | |
4678bdb6 BG |
293 | |
294 | /* Check if we have some sensible values. */ | |
295 | if (eeprom->rev != 'B') | |
296 | /* 'B' is the only supported revision with EEPROM for now. */ | |
297 | return -1; | |
298 | ||
299 | if (eeprom->type != EEPROM_PROBE_TYPE_USB && | |
300 | eeprom->type != EEPROM_PROBE_TYPE_JACK && | |
301 | eeprom->type != EEPROM_PROBE_TYPE_HE10) | |
302 | return -1; | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | /* Some i2c slave addresses on revision B probes differ from revision A. */ | |
308 | static int revB_addr_to_num(unsigned int addr) | |
309 | { | |
310 | switch (addr) { | |
311 | case 0x44: return 5; | |
312 | case 0x45: return 6; | |
313 | case 0x42: return 3; | |
314 | case 0x43: return 4; | |
315 | default: return addr - 0x3f; | |
316 | } | |
317 | } | |
318 | ||
6b80b80d BG |
319 | SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type, |
320 | unsigned int addr, int prb_num) | |
321 | { | |
322 | struct sr_channel_group *cg; | |
323 | struct channel_group_priv *cgp; | |
4678bdb6 BG |
324 | struct probe_eeprom eeprom; |
325 | int hwmon, status; | |
326 | uint32_t gpio; | |
6b80b80d BG |
327 | |
328 | /* Obtain the hwmon index. */ | |
329 | hwmon = get_hwmon_index(addr); | |
330 | if (hwmon < 0) | |
331 | return FALSE; | |
332 | ||
333 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
334 | cgp = g_malloc0(sizeof(struct channel_group_priv)); | |
4678bdb6 BG |
335 | cg->priv = cgp; |
336 | ||
337 | /* | |
338 | * See if we can read the EEPROM contents. If not, assume it's | |
339 | * a revision A probe. | |
340 | */ | |
4c3a4bca | 341 | memset(&eeprom, 0, sizeof(struct probe_eeprom)); |
4678bdb6 BG |
342 | status = read_probe_eeprom(addr, &eeprom); |
343 | cgp->rev = status < 0 ? ACME_REV_A : ACME_REV_B; | |
344 | ||
345 | prb_num = cgp->rev == ACME_REV_A ? prb_num : revB_addr_to_num(addr); | |
346 | ||
6b80b80d | 347 | cgp->hwmon_num = hwmon; |
61f2b7f7 | 348 | cgp->probe_type = type; |
740ad48a | 349 | cgp->index = prb_num - 1; |
6b80b80d | 350 | cg->name = g_strdup_printf("Probe_%d", prb_num); |
4678bdb6 BG |
351 | |
352 | if (cgp->rev == ACME_REV_A) { | |
353 | gpio = revA_pws_info_gpios[cgp->index]; | |
354 | cgp->has_pws = sr_gpio_getval_export(gpio); | |
355 | cgp->pws_gpio = revA_pws_gpios[cgp->index]; | |
356 | } else { | |
357 | cgp->has_pws = eeprom.pwr_sw; | |
358 | cgp->pws_gpio = revB_pws_gpios[cgp->index]; | |
359 | ||
360 | /* | |
361 | * For revision B we can already try to set the shunt | |
362 | * resistance according to the EEPROM contents. | |
363 | * | |
364 | * Keep the default value if shunt in EEPROM == 0. | |
365 | */ | |
366 | if (eeprom.shunt > 0) | |
367 | bl_acme_set_shunt(cg, UOHM_TO_MOHM(eeprom.shunt)); | |
368 | } | |
6b80b80d BG |
369 | |
370 | if (type == PROBE_ENRG) { | |
371 | append_channel(sdi, cg, prb_num, ENRG_PWR); | |
372 | append_channel(sdi, cg, prb_num, ENRG_CURR); | |
373 | append_channel(sdi, cg, prb_num, ENRG_VOL); | |
374 | } else if (type == PROBE_TEMP) { | |
375 | append_channel(sdi, cg, prb_num, TEMP_IN); | |
376 | append_channel(sdi, cg, prb_num, TEMP_OUT); | |
377 | } else { | |
391e23a9 | 378 | sr_err("Invalid probe type: %d.", type); |
6b80b80d BG |
379 | } |
380 | ||
381 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
382 | ||
383 | return TRUE; | |
384 | } | |
385 | ||
1fe04eb8 BG |
386 | SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg) |
387 | { | |
388 | struct channel_group_priv *cgp = cg->priv; | |
389 | ||
390 | return cgp->probe_type; | |
391 | } | |
392 | ||
393 | SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg) | |
394 | { | |
395 | struct channel_group_priv *cgp = cg->priv; | |
396 | ||
8f196120 | 397 | return cgp->has_pws; |
1fe04eb8 BG |
398 | } |
399 | ||
61f2b7f7 BG |
400 | /* |
401 | * Sets path to the hwmon attribute if this channel group | |
402 | * supports shunt resistance setting. The caller has to supply | |
403 | * a valid GString. | |
404 | */ | |
405 | static int get_shunt_path(const struct sr_channel_group *cg, GString *path) | |
406 | { | |
407 | struct channel_group_priv *cgp; | |
408 | int ret = SR_OK, status; | |
409 | ||
410 | cgp = cg->priv; | |
411 | ||
412 | if (cgp->probe_type != PROBE_ENRG) { | |
413 | sr_err("Probe doesn't support shunt resistance setting"); | |
414 | return SR_ERR_ARG; | |
415 | } | |
416 | ||
417 | g_string_append_printf(path, | |
418 | "/sys/class/hwmon/hwmon%d/shunt_resistor", | |
419 | cgp->hwmon_num); | |
420 | ||
421 | /* | |
422 | * The shunt_resistor sysfs attribute is available | |
423 | * in the Linux kernel since version 3.20. We have | |
391e23a9 | 424 | * to notify the user if this attribute is not present. |
61f2b7f7 BG |
425 | */ |
426 | status = g_file_test(path->str, G_FILE_TEST_EXISTS); | |
427 | if (!status) { | |
391e23a9 | 428 | sr_err("shunt_resistance attribute not present, please update " |
61f2b7f7 BG |
429 | "your kernel to version >=3.20"); |
430 | return SR_ERR_NA; | |
431 | } | |
432 | ||
433 | return ret; | |
434 | } | |
435 | ||
7c91c22a BG |
436 | /* |
437 | * Try setting the update_interval sysfs attribute for each probe according | |
438 | * to samplerate. | |
439 | */ | |
440 | SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi, | |
441 | uint64_t samplerate) | |
442 | { | |
443 | struct sr_channel_group *cg; | |
444 | struct channel_group_priv *cgp; | |
445 | GString *hwmon; | |
446 | GSList *l; | |
447 | FILE *fd; | |
448 | ||
449 | for (l = sdi->channel_groups; l != NULL; l = l->next) { | |
450 | cg = l->data; | |
451 | cgp = cg->priv; | |
452 | ||
453 | hwmon = g_string_sized_new(64); | |
454 | g_string_append_printf(hwmon, | |
455 | "/sys/class/hwmon/hwmon%d/update_interval", | |
456 | cgp->hwmon_num); | |
457 | ||
458 | if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) { | |
459 | fd = g_fopen(hwmon->str, "w"); | |
460 | if (!fd) { | |
461 | g_string_free(hwmon, TRUE); | |
462 | continue; | |
463 | } | |
464 | ||
465 | g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate); | |
466 | fclose(fd); | |
467 | } | |
468 | ||
469 | g_string_free(hwmon, TRUE); | |
470 | } | |
471 | } | |
472 | ||
61f2b7f7 BG |
473 | SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg, |
474 | uint64_t *shunt) | |
475 | { | |
476 | GString *path = g_string_sized_new(64); | |
477 | gchar *contents; | |
478 | int status, ret = SR_OK; | |
479 | GError *err = NULL; | |
480 | ||
481 | status = get_shunt_path(cg, path); | |
482 | if (status != SR_OK) { | |
483 | ret = status; | |
484 | goto out; | |
485 | } | |
486 | ||
487 | status = g_file_get_contents(path->str, &contents, NULL, &err); | |
488 | if (!status) { | |
489 | sr_err("Error reading shunt resistance: %s", err->message); | |
490 | ret = SR_ERR_IO; | |
a64fec2b | 491 | g_error_free(err); |
61f2b7f7 BG |
492 | goto out; |
493 | } | |
494 | ||
495 | *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10)); | |
496 | ||
497 | out: | |
498 | g_string_free(path, TRUE); | |
499 | return ret; | |
500 | } | |
501 | ||
391e23a9 | 502 | SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt) |
61f2b7f7 BG |
503 | { |
504 | GString *path = g_string_sized_new(64);; | |
505 | int status, ret = SR_OK; | |
506 | FILE *fd; | |
507 | ||
508 | status = get_shunt_path(cg, path); | |
509 | if (status != SR_OK) { | |
510 | ret = status; | |
511 | goto out; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Can't use g_file_set_contents() here, as it calls open() with | |
516 | * O_EXEC flag in a sysfs directory thus failing with EACCES. | |
517 | */ | |
518 | fd = g_fopen(path->str, "w"); | |
519 | if (!fd) { | |
7237e912 | 520 | sr_err("Error opening %s: %s", path->str, g_strerror(errno)); |
350b8b07 BG |
521 | ret = SR_ERR_IO; |
522 | goto out; | |
61f2b7f7 BG |
523 | } |
524 | ||
34527854 | 525 | g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt)); |
61f2b7f7 BG |
526 | fclose(fd); |
527 | ||
528 | out: | |
529 | g_string_free(path, TRUE); | |
530 | return ret; | |
531 | } | |
532 | ||
740ad48a BG |
533 | SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg, |
534 | gboolean *off) | |
535 | { | |
536 | struct channel_group_priv *cgp; | |
537 | int val; | |
538 | ||
539 | cgp = cg->priv; | |
540 | ||
1fe04eb8 | 541 | if (!bl_acme_probe_has_pws(cg)) { |
740ad48a BG |
542 | sr_err("Probe has no power-switch"); |
543 | return SR_ERR_ARG; | |
544 | } | |
545 | ||
4678bdb6 | 546 | val = sr_gpio_getval_export(cgp->pws_gpio); |
740ad48a BG |
547 | *off = val ? FALSE : TRUE; |
548 | ||
549 | return SR_OK; | |
550 | } | |
551 | ||
552 | SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg, | |
553 | gboolean off) | |
554 | { | |
555 | struct channel_group_priv *cgp; | |
09d217a4 | 556 | int val; |
740ad48a BG |
557 | |
558 | cgp = cg->priv; | |
559 | ||
1fe04eb8 | 560 | if (!bl_acme_probe_has_pws(cg)) { |
740ad48a BG |
561 | sr_err("Probe has no power-switch"); |
562 | return SR_ERR_ARG; | |
563 | } | |
564 | ||
4678bdb6 | 565 | val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1); |
192d37e7 BG |
566 | if (val < 0) { |
567 | sr_err("Error setting power-off state: gpio: %d", | |
4678bdb6 | 568 | cgp->pws_gpio); |
192d37e7 BG |
569 | return SR_ERR_IO; |
570 | } | |
740ad48a BG |
571 | |
572 | return SR_OK; | |
573 | } | |
574 | ||
6b80b80d | 575 | static int channel_to_mq(struct sr_channel *ch) |
dfaee1de | 576 | { |
6b80b80d BG |
577 | struct channel_priv *chp; |
578 | ||
579 | chp = ch->priv; | |
580 | ||
581 | switch (chp->ch_type) { | |
582 | case ENRG_PWR: | |
583 | return SR_MQ_POWER; | |
584 | case ENRG_CURR: | |
585 | return SR_MQ_CURRENT; | |
586 | case ENRG_VOL: | |
587 | return SR_MQ_VOLTAGE; | |
391e23a9 | 588 | case TEMP_IN: /* Fallthrough */ |
6b80b80d BG |
589 | case TEMP_OUT: |
590 | return SR_MQ_TEMPERATURE; | |
591 | default: | |
592 | return -1; | |
593 | } | |
594 | } | |
595 | ||
596 | static int channel_to_unit(struct sr_channel *ch) | |
597 | { | |
598 | struct channel_priv *chp; | |
599 | ||
600 | chp = ch->priv; | |
601 | ||
602 | switch (chp->ch_type) { | |
603 | case ENRG_PWR: | |
604 | return SR_UNIT_WATT; | |
605 | case ENRG_CURR: | |
606 | return SR_UNIT_AMPERE; | |
607 | case ENRG_VOL: | |
608 | return SR_UNIT_VOLT; | |
391e23a9 | 609 | case TEMP_IN: /* Fallthrough */ |
6b80b80d BG |
610 | case TEMP_OUT: |
611 | return SR_UNIT_CELSIUS; | |
612 | default: | |
613 | return -1; | |
614 | } | |
615 | } | |
616 | ||
617 | /* We need to scale measurements down from the units used by the drivers. */ | |
ad4c0275 | 618 | static int type_digits(int type) |
6b80b80d BG |
619 | { |
620 | switch (type) { | |
621 | case ENRG_PWR: | |
ad4c0275 | 622 | return 6; |
391e23a9 UH |
623 | case ENRG_CURR: /* Fallthrough */ |
624 | case ENRG_VOL: /* Fallthrough */ | |
625 | case TEMP_IN: /* Fallthrough */ | |
6b80b80d | 626 | case TEMP_OUT: |
ad4c0275 | 627 | return 3; |
6b80b80d | 628 | default: |
ad4c0275 | 629 | return 0; |
6b80b80d BG |
630 | } |
631 | } | |
632 | ||
633 | static float read_sample(struct sr_channel *ch) | |
634 | { | |
635 | struct channel_priv *chp; | |
4e88b86c | 636 | char buf[16]; |
6b80b80d BG |
637 | ssize_t len; |
638 | int fd; | |
639 | ||
4e88b86c DL |
640 | chp = ch->priv; |
641 | fd = chp->fd; | |
642 | ||
643 | lseek(fd, 0, SEEK_SET); | |
644 | ||
645 | len = read(fd, buf, sizeof(buf)); | |
646 | if (len < 0) { | |
6433156c | 647 | sr_err("Error reading from channel %s (hwmon: %d): %s", |
7237e912 | 648 | ch->name, chp->probe->hwmon_num, g_strerror(errno)); |
4e88b86c DL |
649 | ch->enabled = FALSE; |
650 | return -1.0; | |
651 | } | |
652 | ||
ad4c0275 AJ |
653 | chp->digits = type_digits(chp->ch_type); |
654 | return strtol(buf, NULL, 10) * powf(10, -chp->digits); | |
4e88b86c DL |
655 | } |
656 | ||
657 | SR_PRIV int bl_acme_open_channel(struct sr_channel *ch) | |
658 | { | |
659 | struct channel_priv *chp; | |
2c240774 UH |
660 | char path[64]; |
661 | const char *file; | |
4e88b86c DL |
662 | int fd; |
663 | ||
6b80b80d BG |
664 | chp = ch->priv; |
665 | ||
666 | switch (chp->ch_type) { | |
667 | case ENRG_PWR: file = "power1_input"; break; | |
668 | case ENRG_CURR: file = "curr1_input"; break; | |
669 | case ENRG_VOL: file = "in1_input"; break; | |
670 | case TEMP_IN: file = "temp1_input"; break; | |
671 | case TEMP_OUT: file = "temp2_input"; break; | |
672 | default: | |
391e23a9 | 673 | sr_err("Invalid channel type: %d.", chp->ch_type); |
4e88b86c | 674 | return SR_ERR; |
6b80b80d BG |
675 | } |
676 | ||
391e23a9 | 677 | snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s", |
6b80b80d | 678 | chp->probe->hwmon_num, file); |
4e88b86c | 679 | |
6b80b80d BG |
680 | fd = open(path, O_RDONLY); |
681 | if (fd < 0) { | |
7237e912 | 682 | sr_err("Error opening %s: %s", path, g_strerror(errno)); |
6b80b80d | 683 | ch->enabled = FALSE; |
4e88b86c | 684 | return SR_ERR; |
6b80b80d BG |
685 | } |
686 | ||
4e88b86c | 687 | chp->fd = fd; |
6b80b80d | 688 | |
4e88b86c DL |
689 | return 0; |
690 | } | |
691 | ||
692 | SR_PRIV void bl_acme_close_channel(struct sr_channel *ch) | |
693 | { | |
694 | struct channel_priv *chp; | |
695 | ||
696 | chp = ch->priv; | |
697 | close(chp->fd); | |
698 | chp->fd = -1; | |
6b80b80d BG |
699 | } |
700 | ||
701 | SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data) | |
702 | { | |
a0648b1a | 703 | uint64_t nrexpiration; |
6b80b80d | 704 | struct sr_datafeed_packet packet, framep; |
63353141 UH |
705 | struct sr_datafeed_analog analog; |
706 | struct sr_analog_encoding encoding; | |
707 | struct sr_analog_meaning meaning; | |
708 | struct sr_analog_spec spec; | |
6b80b80d BG |
709 | struct sr_dev_inst *sdi; |
710 | struct sr_channel *ch; | |
7e5a048f | 711 | struct channel_priv *chp; |
dfaee1de | 712 | struct dev_context *devc; |
6b80b80d | 713 | GSList *chl, chonly; |
7e5a048f | 714 | unsigned i; |
dfaee1de BG |
715 | |
716 | (void)fd; | |
6b80b80d BG |
717 | (void)revents; |
718 | ||
719 | sdi = cb_data; | |
720 | if (!sdi) | |
721 | return TRUE; | |
dfaee1de | 722 | |
6b80b80d BG |
723 | devc = sdi->priv; |
724 | if (!devc) | |
dfaee1de BG |
725 | return TRUE; |
726 | ||
63353141 | 727 | packet.type = SR_DF_ANALOG; |
6b80b80d | 728 | packet.payload = &analog; |
63353141 | 729 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
6b80b80d | 730 | |
a0648b1a DL |
731 | if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) { |
732 | sr_warn("Failed to read timer information"); | |
733 | return TRUE; | |
734 | } | |
735 | ||
6b80b80d | 736 | /* |
a0648b1a DL |
737 | * We were not able to process the previous timer expiration, we are |
738 | * overloaded. | |
6b80b80d | 739 | */ |
a0648b1a DL |
740 | if (nrexpiration > 1) |
741 | devc->samples_missed += nrexpiration - 1; | |
6b80b80d | 742 | |
6b80b80d | 743 | /* |
7e5a048f BG |
744 | * XXX This is a nasty workaround... |
745 | * | |
746 | * At high sampling rates and maximum channels we are not able to | |
747 | * acquire samples fast enough, even though frontends still think | |
748 | * that samples arrive on time. This causes shifts in frontend | |
749 | * plots. | |
750 | * | |
751 | * To compensate for the delay we check if any clock events were | |
752 | * missed and - if so - don't really read the next value, but send | |
753 | * the same sample as fast as possible. We do it until we are back | |
754 | * on schedule. | |
755 | * | |
756 | * At high sampling rate this doesn't seem to visibly reduce the | |
757 | * accuracy. | |
6b80b80d | 758 | */ |
7e5a048f BG |
759 | for (i = 0; i < nrexpiration; i++) { |
760 | framep.type = SR_DF_FRAME_BEGIN; | |
695dc859 | 761 | sr_session_send(sdi, &framep); |
7e5a048f BG |
762 | |
763 | /* | |
764 | * Due to different units used in each channel we're sending | |
765 | * samples one-by-one. | |
766 | */ | |
767 | for (chl = sdi->channels; chl; chl = chl->next) { | |
768 | ch = chl->data; | |
769 | chp = ch->priv; | |
6b80b80d | 770 | |
7e5a048f BG |
771 | if (!ch->enabled) |
772 | continue; | |
773 | chonly.next = NULL; | |
774 | chonly.data = ch; | |
7e5a048f | 775 | analog.num_samples = 1; |
63353141 UH |
776 | analog.meaning->channels = &chonly; |
777 | analog.meaning->mq = channel_to_mq(chl->data); | |
778 | analog.meaning->unit = channel_to_unit(ch); | |
7e5a048f BG |
779 | |
780 | if (i < 1) | |
781 | chp->val = read_sample(ch); | |
782 | ||
ad4c0275 AJ |
783 | analog.encoding->digits = chp->digits; |
784 | analog.spec->spec_digits = chp->digits; | |
7e5a048f | 785 | analog.data = &chp->val; |
695dc859 | 786 | sr_session_send(sdi, &packet); |
7e5a048f BG |
787 | } |
788 | ||
789 | framep.type = SR_DF_FRAME_END; | |
695dc859 | 790 | sr_session_send(sdi, &framep); |
7e5a048f | 791 | } |
6b80b80d | 792 | |
d54a7c42 LPC |
793 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
794 | ||
795 | if (sr_sw_limits_check(&devc->limits)) { | |
695dc859 | 796 | sdi->driver->dev_acquisition_stop(sdi); |
dfaee1de | 797 | return TRUE; |
dfaee1de BG |
798 | } |
799 | ||
800 | return TRUE; | |
801 | } |