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