]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/protocol.c
baylibre-acme: Optimize reading of values from sysfs.
[libsigrok.git] / src / hardware / baylibre-acme / protocol.c
CommitLineData
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
6b80b80d 20#include <string.h>
391e23a9 21#include <stdlib.h>
6b80b80d 22#include <errno.h>
391e23a9 23#include <fcntl.h>
61f2b7f7 24#include <glib/gstdio.h>
dfaee1de 25#include "protocol.h"
740ad48a 26#include "gpio.h"
dfaee1de 27
6b80b80d
BG
28struct channel_group_priv {
29 int hwmon_num;
61f2b7f7 30 int probe_type;
740ad48a 31 int index;
6b80b80d
BG
32};
33
34struct channel_priv {
35 int ch_type;
4e88b86c 36 int fd;
6b80b80d
BG
37 struct channel_group_priv *probe;
38};
39
40static const uint8_t enrg_i2c_addrs[] = {
41 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
42};
43
44static const uint8_t temp_i2c_addrs[] = {
45 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
46};
47
740ad48a 48static const uint32_t pws_gpios[] = {
391e23a9 49 486, 498, 502, 482, 478, 506, 510, 474,
740ad48a
BG
50};
51
52static const uint32_t pws_info_gpios[] = {
53 487, 499, 503, 483, 479, 507, 511, 475,
54};
55
61f2b7f7
BG
56#define MOHM_TO_UOHM(x) ((x) * 1000)
57#define UOHM_TO_MOHM(x) ((x) / 1000)
58
6b80b80d
BG
59SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
60{
61 return enrg_i2c_addrs[index];
62}
63
64SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
65{
66 return temp_i2c_addrs[index];
67}
68
69SR_PRIV gboolean bl_acme_is_sane(void)
70{
71 gboolean status;
72
73 /*
74 * We expect sysfs to be present and mounted at /sys, ina226 and
75 * tmp435 sensors detected by the system and their appropriate
76 * drivers loaded and functional.
77 */
78 status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
79 if (!status) {
80 sr_err("/sys/ directory not found - sysfs not mounted?");
81 return FALSE;
82 }
83
84 return TRUE;
85}
86
87static void probe_name_path(unsigned int addr, GString *path)
88{
89 g_string_printf(path,
90 "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
91}
92
93/*
94 * For given address fill buf with the path to appropriate hwmon entry.
95 */
96static void probe_hwmon_path(unsigned int addr, GString *path)
97{
98 g_string_printf(path,
99 "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
100}
101
102SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
103 int prb_num, const char *prb_name)
104{
105 gboolean ret = FALSE, status;
106 char *buf = NULL;
107 GString *path = g_string_sized_new(64);
108 GError *err = NULL;
109 gsize size;
110
111 probe_name_path(addr, path);
112 status = g_file_get_contents(path->str, &buf, &size, &err);
113 if (!status) {
114 sr_dbg("Name for probe %d can't be read: %s",
115 prb_num, err->message);
116 g_string_free(path, TRUE);
117 return ret;
6b80b80d
BG
118 }
119
391e23a9 120 if (!strncmp(buf, prb_name, strlen(prb_name))) {
6b80b80d
BG
121 /*
122 * Correct driver registered on this address - but is
123 * there an actual probe connected?
124 */
125 probe_hwmon_path(addr, path);
126 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
127 if (status) {
128 /* We have found an ACME probe. */
129 ret = TRUE;
130 }
131 }
132
133 g_free(buf);
134 g_string_free(path, TRUE);
135
136 return ret;
137}
138
139static int get_hwmon_index(unsigned int addr)
140{
141 int status, hwmon;
142 GString *path = g_string_sized_new(64);
143 GError *err = NULL;
144 GDir *dir;
145
146 probe_hwmon_path(addr, path);
147 dir = g_dir_open(path->str, 0, &err);
391e23a9 148 if (!dir) {
6b80b80d
BG
149 sr_err("Error opening %s: %s", path->str, err->message);
150 g_string_free(path, TRUE);
151 return -1;
152 }
153
154 g_string_free(path, TRUE);
155
156 /*
157 * The directory should contain a single file named hwmonX where X
158 * is the hwmon index.
159 */
160 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
161 g_dir_close(dir);
162 if (status != 1) {
163 sr_err("Unable to determine the hwmon entry");
164 return -1;
165 }
166
167 return hwmon;
168}
169
391e23a9 170static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
6b80b80d
BG
171 int index, int type)
172{
173 struct channel_priv *cp;
174 struct dev_context *devc;
175 struct sr_channel *ch;
176 char *name;
177
178 devc = sdi->priv;
179
180 switch (type) {
181 case ENRG_PWR:
182 name = g_strdup_printf("P%d_ENRG_PWR", index);
183 break;
184 case ENRG_CURR:
185 name = g_strdup_printf("P%d_ENRG_CURR", index);
186 break;
187 case ENRG_VOL:
188 name = g_strdup_printf("P%d_ENRG_VOL", index);
189 break;
190 case TEMP_IN:
191 name = g_strdup_printf("P%d_TEMP_IN", index);
192 break;
193 case TEMP_OUT:
194 name = g_strdup_printf("P%d_TEMP_OUT", index);
195 break;
196 default:
391e23a9 197 sr_err("Invalid channel type: %d.", type);
6b80b80d
BG
198 return;
199 }
200
201 cp = g_malloc0(sizeof(struct channel_priv));
202 cp->ch_type = type;
203 cp->probe = cg->priv;
204
5e23fcab 205 ch = sr_channel_new(sdi, devc->num_channels++,
6b80b80d
BG
206 SR_CHANNEL_ANALOG, TRUE, name);
207 g_free(name);
208
209 ch->priv = cp;
210 cg->channels = g_slist_append(cg->channels, ch);
6b80b80d
BG
211}
212
213SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
214 unsigned int addr, int prb_num)
215{
216 struct sr_channel_group *cg;
217 struct channel_group_priv *cgp;
218 int hwmon;
219
220 /* Obtain the hwmon index. */
221 hwmon = get_hwmon_index(addr);
222 if (hwmon < 0)
223 return FALSE;
224
225 cg = g_malloc0(sizeof(struct sr_channel_group));
226 cgp = g_malloc0(sizeof(struct channel_group_priv));
227 cgp->hwmon_num = hwmon;
61f2b7f7 228 cgp->probe_type = type;
740ad48a 229 cgp->index = prb_num - 1;
6b80b80d
BG
230 cg->name = g_strdup_printf("Probe_%d", prb_num);
231 cg->priv = cgp;
232
233 if (type == PROBE_ENRG) {
234 append_channel(sdi, cg, prb_num, ENRG_PWR);
235 append_channel(sdi, cg, prb_num, ENRG_CURR);
236 append_channel(sdi, cg, prb_num, ENRG_VOL);
237 } else if (type == PROBE_TEMP) {
238 append_channel(sdi, cg, prb_num, TEMP_IN);
239 append_channel(sdi, cg, prb_num, TEMP_OUT);
240 } else {
391e23a9 241 sr_err("Invalid probe type: %d.", type);
6b80b80d
BG
242 }
243
244 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
245
246 return TRUE;
247}
248
1fe04eb8
BG
249SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg)
250{
251 struct channel_group_priv *cgp = cg->priv;
252
253 return cgp->probe_type;
254}
255
256SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
257{
258 struct channel_group_priv *cgp = cg->priv;
259
260 return sr_gpio_getval_export(pws_info_gpios[cgp->index]);
261}
262
61f2b7f7
BG
263/*
264 * Sets path to the hwmon attribute if this channel group
265 * supports shunt resistance setting. The caller has to supply
266 * a valid GString.
267 */
268static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
269{
270 struct channel_group_priv *cgp;
271 int ret = SR_OK, status;
272
273 cgp = cg->priv;
274
275 if (cgp->probe_type != PROBE_ENRG) {
276 sr_err("Probe doesn't support shunt resistance setting");
277 return SR_ERR_ARG;
278 }
279
280 g_string_append_printf(path,
281 "/sys/class/hwmon/hwmon%d/shunt_resistor",
282 cgp->hwmon_num);
283
284 /*
285 * The shunt_resistor sysfs attribute is available
286 * in the Linux kernel since version 3.20. We have
391e23a9 287 * to notify the user if this attribute is not present.
61f2b7f7
BG
288 */
289 status = g_file_test(path->str, G_FILE_TEST_EXISTS);
290 if (!status) {
391e23a9 291 sr_err("shunt_resistance attribute not present, please update "
61f2b7f7
BG
292 "your kernel to version >=3.20");
293 return SR_ERR_NA;
294 }
295
296 return ret;
297}
298
7c91c22a
BG
299/*
300 * Try setting the update_interval sysfs attribute for each probe according
301 * to samplerate.
302 */
303SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi,
304 uint64_t samplerate)
305{
306 struct sr_channel_group *cg;
307 struct channel_group_priv *cgp;
308 GString *hwmon;
309 GSList *l;
310 FILE *fd;
311
312 for (l = sdi->channel_groups; l != NULL; l = l->next) {
313 cg = l->data;
314 cgp = cg->priv;
315
316 hwmon = g_string_sized_new(64);
317 g_string_append_printf(hwmon,
318 "/sys/class/hwmon/hwmon%d/update_interval",
319 cgp->hwmon_num);
320
321 if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) {
322 fd = g_fopen(hwmon->str, "w");
323 if (!fd) {
324 g_string_free(hwmon, TRUE);
325 continue;
326 }
327
328 g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate);
329 fclose(fd);
330 }
331
332 g_string_free(hwmon, TRUE);
333 }
334}
335
61f2b7f7
BG
336SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
337 uint64_t *shunt)
338{
339 GString *path = g_string_sized_new(64);
340 gchar *contents;
341 int status, ret = SR_OK;
342 GError *err = NULL;
343
344 status = get_shunt_path(cg, path);
345 if (status != SR_OK) {
346 ret = status;
347 goto out;
348 }
349
350 status = g_file_get_contents(path->str, &contents, NULL, &err);
351 if (!status) {
352 sr_err("Error reading shunt resistance: %s", err->message);
353 ret = SR_ERR_IO;
354 goto out;
355 }
356
357 *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
358
359out:
360 g_string_free(path, TRUE);
361 return ret;
362}
363
391e23a9 364SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
61f2b7f7
BG
365{
366 GString *path = g_string_sized_new(64);;
367 int status, ret = SR_OK;
368 FILE *fd;
369
370 status = get_shunt_path(cg, path);
371 if (status != SR_OK) {
372 ret = status;
373 goto out;
374 }
375
376 /*
377 * Can't use g_file_set_contents() here, as it calls open() with
378 * O_EXEC flag in a sysfs directory thus failing with EACCES.
379 */
380 fd = g_fopen(path->str, "w");
381 if (!fd) {
382 sr_err("Error opening %s: %s", path->str, strerror(errno));
350b8b07
BG
383 ret = SR_ERR_IO;
384 goto out;
61f2b7f7
BG
385 }
386
34527854 387 g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
61f2b7f7
BG
388 fclose(fd);
389
390out:
391 g_string_free(path, TRUE);
392 return ret;
393}
394
740ad48a
BG
395SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
396 gboolean *off)
397{
398 struct channel_group_priv *cgp;
399 int val;
400
401 cgp = cg->priv;
402
1fe04eb8 403 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
404 sr_err("Probe has no power-switch");
405 return SR_ERR_ARG;
406 }
407
408 val = sr_gpio_getval_export(pws_gpios[cgp->index]);
409 *off = val ? FALSE : TRUE;
410
411 return SR_OK;
412}
413
414SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
415 gboolean off)
416{
417 struct channel_group_priv *cgp;
09d217a4 418 int val;
740ad48a
BG
419
420 cgp = cg->priv;
421
1fe04eb8 422 if (!bl_acme_probe_has_pws(cg)) {
740ad48a
BG
423 sr_err("Probe has no power-switch");
424 return SR_ERR_ARG;
425 }
426
09d217a4 427 val = sr_gpio_setval_export(pws_gpios[cgp->index], off ? 0 : 1);
192d37e7
BG
428 if (val < 0) {
429 sr_err("Error setting power-off state: gpio: %d",
430 pws_gpios[cgp->index]);
431 return SR_ERR_IO;
432 }
740ad48a
BG
433
434 return SR_OK;
435}
436
6b80b80d 437static int channel_to_mq(struct sr_channel *ch)
dfaee1de 438{
6b80b80d
BG
439 struct channel_priv *chp;
440
441 chp = ch->priv;
442
443 switch (chp->ch_type) {
444 case ENRG_PWR:
445 return SR_MQ_POWER;
446 case ENRG_CURR:
447 return SR_MQ_CURRENT;
448 case ENRG_VOL:
449 return SR_MQ_VOLTAGE;
391e23a9 450 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
451 case TEMP_OUT:
452 return SR_MQ_TEMPERATURE;
453 default:
454 return -1;
455 }
456}
457
458static int channel_to_unit(struct sr_channel *ch)
459{
460 struct channel_priv *chp;
461
462 chp = ch->priv;
463
464 switch (chp->ch_type) {
465 case ENRG_PWR:
466 return SR_UNIT_WATT;
467 case ENRG_CURR:
468 return SR_UNIT_AMPERE;
469 case ENRG_VOL:
470 return SR_UNIT_VOLT;
391e23a9 471 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
472 case TEMP_OUT:
473 return SR_UNIT_CELSIUS;
474 default:
475 return -1;
476 }
477}
478
479/* We need to scale measurements down from the units used by the drivers. */
480static float adjust_data(int val, int type)
481{
482 switch (type) {
483 case ENRG_PWR:
484 return ((float)val) / 1000000.0;
391e23a9
UH
485 case ENRG_CURR: /* Fallthrough */
486 case ENRG_VOL: /* Fallthrough */
487 case TEMP_IN: /* Fallthrough */
6b80b80d
BG
488 case TEMP_OUT:
489 return ((float)val) / 1000.0;
490 default:
491 return 0.0;
492 }
493}
494
495static float read_sample(struct sr_channel *ch)
496{
497 struct channel_priv *chp;
4e88b86c 498 char buf[16];
6b80b80d
BG
499 ssize_t len;
500 int fd;
501
4e88b86c
DL
502 chp = ch->priv;
503 fd = chp->fd;
504
505 lseek(fd, 0, SEEK_SET);
506
507 len = read(fd, buf, sizeof(buf));
508 if (len < 0) {
509 sr_err("Error reading from channel %s (hwmon: %s): %s",
510 ch->name, chp->probe->hwmon_num, strerror(errno));
511 ch->enabled = FALSE;
512 return -1.0;
513 }
514
515 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
516}
517
518SR_PRIV int bl_acme_open_channel(struct sr_channel *ch)
519{
520 struct channel_priv *chp;
521 char path[64], *file;
522 int fd;
523
6b80b80d
BG
524 chp = ch->priv;
525
526 switch (chp->ch_type) {
527 case ENRG_PWR: file = "power1_input"; break;
528 case ENRG_CURR: file = "curr1_input"; break;
529 case ENRG_VOL: file = "in1_input"; break;
530 case TEMP_IN: file = "temp1_input"; break;
531 case TEMP_OUT: file = "temp2_input"; break;
532 default:
391e23a9 533 sr_err("Invalid channel type: %d.", chp->ch_type);
4e88b86c 534 return SR_ERR;
6b80b80d
BG
535 }
536
391e23a9 537 snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
6b80b80d 538 chp->probe->hwmon_num, file);
4e88b86c 539
6b80b80d
BG
540 fd = open(path, O_RDONLY);
541 if (fd < 0) {
542 sr_err("Error opening %s: %s", path, strerror(errno));
543 ch->enabled = FALSE;
4e88b86c 544 return SR_ERR;
6b80b80d
BG
545 }
546
4e88b86c 547 chp->fd = fd;
6b80b80d 548
4e88b86c
DL
549 return 0;
550}
551
552SR_PRIV void bl_acme_close_channel(struct sr_channel *ch)
553{
554 struct channel_priv *chp;
555
556 chp = ch->priv;
557 close(chp->fd);
558 chp->fd = -1;
6b80b80d
BG
559}
560
561SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
562{
563 uint32_t cur_time, elapsed_time, diff_time;
564 int64_t time_to_sleep;
565 struct sr_datafeed_packet packet, framep;
566 struct sr_datafeed_analog analog;
567 struct sr_dev_inst *sdi;
568 struct sr_channel *ch;
dfaee1de 569 struct dev_context *devc;
6b80b80d
BG
570 GSList *chl, chonly;
571 float valf;
dfaee1de
BG
572
573 (void)fd;
6b80b80d
BG
574 (void)revents;
575
576 sdi = cb_data;
577 if (!sdi)
578 return TRUE;
dfaee1de 579
6b80b80d
BG
580 devc = sdi->priv;
581 if (!devc)
dfaee1de
BG
582 return TRUE;
583
6b80b80d
BG
584 packet.type = SR_DF_ANALOG;
585 packet.payload = &analog;
d0148a50 586 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
6b80b80d
BG
587 analog.data = &valf;
588
589 /*
391e23a9 590 * Reading from sysfs takes some time - try to keep up with samplerate.
6b80b80d
BG
591 */
592 if (devc->samples_read) {
593 cur_time = g_get_monotonic_time();
594 diff_time = cur_time - devc->last_sample_fin;
595 time_to_sleep = G_USEC_PER_SEC / devc->samplerate - diff_time;
596 if (time_to_sleep > 0)
597 g_usleep(time_to_sleep);
598 }
599
600 framep.type = SR_DF_FRAME_BEGIN;
601 sr_session_send(cb_data, &framep);
602
603 /*
604 * Due to different units used in each channel we're sending
605 * samples one-by-one.
606 */
607 for (chl = sdi->channels; chl; chl = chl->next) {
608 ch = chl->data;
609 if (!ch->enabled)
610 continue;
611 chonly.next = NULL;
612 chonly.data = ch;
613 analog.channels = &chonly;
614 analog.num_samples = 1;
615 analog.mq = channel_to_mq(chl->data);
616 analog.unit = channel_to_unit(ch);
617
618 valf = read_sample(ch);
619
620 sr_session_send(cb_data, &packet);
621 }
622
623 framep.type = SR_DF_FRAME_END;
624 sr_session_send(cb_data, &framep);
625
626 devc->samples_read++;
627 if (devc->limit_samples > 0 &&
628 devc->samples_read >= devc->limit_samples) {
629 sr_info("Requested number of samples reached.");
630 sdi->driver->dev_acquisition_stop(sdi, cb_data);
631 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de 632 return TRUE;
6b80b80d
BG
633 } else if (devc->limit_msec > 0) {
634 cur_time = g_get_monotonic_time();
635 elapsed_time = cur_time - devc->start_time;
dfaee1de 636
6b80b80d
BG
637 if (elapsed_time >= devc->limit_msec) {
638 sr_info("Sampling time limit reached.");
639 sdi->driver->dev_acquisition_stop(sdi, cb_data);
640 devc->last_sample_fin = g_get_monotonic_time();
641 return TRUE;
642 }
dfaee1de
BG
643 }
644
6b80b80d 645 devc->last_sample_fin = g_get_monotonic_time();
dfaee1de
BG
646 return TRUE;
647}