2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
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.
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.
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/>.
25 #include <arpa/inet.h>
26 #include <glib/gstdio.h>
41 struct channel_group_priv {
54 struct channel_group_priv *probe;
57 #define EEPROM_SERIAL_SIZE 16
58 #define EEPROM_TAG_SIZE 32
60 #define EEPROM_PROBE_TYPE_USB 1
61 #define EEPROM_PROBE_TYPE_JACK 2
62 #define EEPROM_PROBE_TYPE_HE10 3
69 uint8_t serial[EEPROM_SERIAL_SIZE];
70 int8_t tag[EEPROM_TAG_SIZE];
73 #define EEPROM_SIZE (3 * sizeof(uint32_t) + 1 + EEPROM_SERIAL_SIZE + EEPROM_TAG_SIZE)
75 #define EEPROM_OFF_TYPE 0
76 #define EEPROM_OFF_REV sizeof(uint32_t)
77 #define EEPROM_OFF_SHUNT (2 * sizeof(uint32_t))
78 #define EEPROM_OFF_PWR_SW (3 * sizeof(uint32_t))
79 #define EEPROM_OFF_SERIAL (3 * sizeof(uint32_t) + 1)
80 #define EEPROM_OFF_TAG (EEPROM_OFF_SERIAL + EEPROM_SERIAL_SIZE)
82 static const uint8_t enrg_i2c_addrs[] = {
83 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
86 static const uint8_t temp_i2c_addrs[] = {
87 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
90 static const uint32_t revA_pws_gpios[] = {
91 486, 498, 502, 482, 478, 506, 510, 474,
94 static const uint32_t revA_pws_info_gpios[] = {
95 487, 499, 503, 483, 479, 507, 511, 475,
98 static const uint32_t revB_pws_gpios[] = {
99 489, 491, 493, 495, 497, 499, 501, 503,
102 #define MOHM_TO_UOHM(x) ((x) * 1000)
103 #define UOHM_TO_MOHM(x) ((x) / 1000)
105 SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
107 return enrg_i2c_addrs[index];
110 SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
112 return temp_i2c_addrs[index];
115 SR_PRIV gboolean bl_acme_is_sane(void)
120 * We expect sysfs to be present and mounted at /sys, ina226 and
121 * tmp435 sensors detected by the system and their appropriate
122 * drivers loaded and functional.
124 status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
126 sr_err("/sys/ directory not found - sysfs not mounted?");
133 static void probe_name_path(unsigned int addr, GString *path)
135 g_string_printf(path,
136 "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
140 * For given address fill buf with the path to appropriate hwmon entry.
142 static void probe_hwmon_path(unsigned int addr, GString *path)
144 g_string_printf(path,
145 "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
148 static void probe_eeprom_path(unsigned int addr, GString *path)
150 g_string_printf(path,
151 "/sys/class/i2c-dev/i2c-1/device/1-00%02x/eeprom",
155 SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
156 int prb_num, const char *prb_name)
158 gboolean ret = FALSE, status;
160 GString *path = g_string_sized_new(64);
164 probe_name_path(addr, path);
165 status = g_file_get_contents(path->str, &buf, &size, &err);
167 sr_dbg("Name for probe %d can't be read: %s",
168 prb_num, err->message);
169 g_string_free(path, TRUE);
173 if (!strncmp(buf, prb_name, strlen(prb_name))) {
175 * Correct driver registered on this address - but is
176 * there an actual probe connected?
178 probe_hwmon_path(addr, path);
179 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
181 /* We have found an ACME probe. */
187 g_string_free(path, TRUE);
192 static int get_hwmon_index(unsigned int addr)
195 GString *path = g_string_sized_new(64);
199 probe_hwmon_path(addr, path);
200 dir = g_dir_open(path->str, 0, &err);
202 sr_err("Error opening %s: %s", path->str, err->message);
203 g_string_free(path, TRUE);
207 g_string_free(path, TRUE);
210 * The directory should contain a single file named hwmonX where X
211 * is the hwmon index.
213 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
216 sr_err("Unable to determine the hwmon entry");
223 static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
226 struct channel_priv *cp;
227 struct dev_context *devc;
228 struct sr_channel *ch;
235 name = g_strdup_printf("P%d_ENRG_PWR", index);
238 name = g_strdup_printf("P%d_ENRG_CURR", index);
241 name = g_strdup_printf("P%d_ENRG_VOL", index);
244 name = g_strdup_printf("P%d_TEMP_IN", index);
247 name = g_strdup_printf("P%d_TEMP_OUT", index);
250 sr_err("Invalid channel type: %d.", type);
254 cp = g_malloc0(sizeof(struct channel_priv));
256 cp->probe = cg->priv;
258 ch = sr_channel_new(sdi, devc->num_channels++,
259 SR_CHANNEL_ANALOG, TRUE, name);
263 cg->channels = g_slist_append(cg->channels, ch);
266 static int read_probe_eeprom(unsigned int addr, struct probe_eeprom *eeprom)
268 GString *path = g_string_sized_new(64);
269 char eeprom_buf[EEPROM_SIZE];
273 probe_eeprom_path(addr, path);
274 fd = g_open(path->str, O_RDONLY);
275 g_string_free(path, TRUE);
279 rd = read(fd, eeprom_buf, EEPROM_SIZE);
281 if (rd != EEPROM_SIZE)
284 eeprom->type = RB32(eeprom_buf + EEPROM_OFF_TYPE);
285 eeprom->rev = RB32(eeprom_buf + EEPROM_OFF_REV);
286 eeprom->shunt = RB32(eeprom_buf + EEPROM_OFF_SHUNT);
287 eeprom->pwr_sw = R8(eeprom_buf + EEPROM_OFF_PWR_SW);
288 /* Don't care about the serial number and tag for now. */
290 /* Check if we have some sensible values. */
291 if (eeprom->rev != 'B')
292 /* 'B' is the only supported revision with EEPROM for now. */
295 if (eeprom->type != EEPROM_PROBE_TYPE_USB &&
296 eeprom->type != EEPROM_PROBE_TYPE_JACK &&
297 eeprom->type != EEPROM_PROBE_TYPE_HE10)
303 /* Some i2c slave addresses on revision B probes differ from revision A. */
304 static int revB_addr_to_num(unsigned int addr)
311 default: return addr - 0x3f;
315 SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
316 unsigned int addr, int prb_num)
318 struct sr_channel_group *cg;
319 struct channel_group_priv *cgp;
320 struct probe_eeprom eeprom;
324 /* Obtain the hwmon index. */
325 hwmon = get_hwmon_index(addr);
329 cg = g_malloc0(sizeof(struct sr_channel_group));
330 cgp = g_malloc0(sizeof(struct channel_group_priv));
334 * See if we can read the EEPROM contents. If not, assume it's
335 * a revision A probe.
337 memset(&eeprom, 0, sizeof(struct probe_eeprom));
338 status = read_probe_eeprom(addr, &eeprom);
339 cgp->rev = status < 0 ? ACME_REV_A : ACME_REV_B;
341 prb_num = cgp->rev == ACME_REV_A ? prb_num : revB_addr_to_num(addr);
343 cgp->hwmon_num = hwmon;
344 cgp->probe_type = type;
345 cgp->index = prb_num - 1;
346 cg->name = g_strdup_printf("Probe_%d", prb_num);
348 if (cgp->rev == ACME_REV_A) {
349 gpio = revA_pws_info_gpios[cgp->index];
350 cgp->has_pws = sr_gpio_getval_export(gpio);
351 cgp->pws_gpio = revA_pws_gpios[cgp->index];
353 cgp->has_pws = eeprom.pwr_sw;
354 cgp->pws_gpio = revB_pws_gpios[cgp->index];
357 * For revision B we can already try to set the shunt
358 * resistance according to the EEPROM contents.
360 * Keep the default value if shunt in EEPROM == 0.
362 if (eeprom.shunt > 0)
363 bl_acme_set_shunt(cg, UOHM_TO_MOHM(eeprom.shunt));
366 if (type == PROBE_ENRG) {
367 append_channel(sdi, cg, prb_num, ENRG_PWR);
368 append_channel(sdi, cg, prb_num, ENRG_CURR);
369 append_channel(sdi, cg, prb_num, ENRG_VOL);
370 } else if (type == PROBE_TEMP) {
371 append_channel(sdi, cg, prb_num, TEMP_IN);
372 append_channel(sdi, cg, prb_num, TEMP_OUT);
374 sr_err("Invalid probe type: %d.", type);
377 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
382 SR_PRIV int bl_acme_get_probe_type(const struct sr_channel_group *cg)
384 struct channel_group_priv *cgp = cg->priv;
386 return cgp->probe_type;
389 SR_PRIV int bl_acme_probe_has_pws(const struct sr_channel_group *cg)
391 struct channel_group_priv *cgp = cg->priv;
397 * Sets path to the hwmon attribute if this channel group
398 * supports shunt resistance setting. The caller has to supply
401 static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
403 struct channel_group_priv *cgp;
404 int ret = SR_OK, status;
408 if (cgp->probe_type != PROBE_ENRG) {
409 sr_err("Probe doesn't support shunt resistance setting");
413 g_string_append_printf(path,
414 "/sys/class/hwmon/hwmon%d/shunt_resistor",
418 * The shunt_resistor sysfs attribute is available
419 * in the Linux kernel since version 3.20. We have
420 * to notify the user if this attribute is not present.
422 status = g_file_test(path->str, G_FILE_TEST_EXISTS);
424 sr_err("shunt_resistance attribute not present, please update "
425 "your kernel to version >=3.20");
433 * Try setting the update_interval sysfs attribute for each probe according
436 SR_PRIV void bl_acme_maybe_set_update_interval(const struct sr_dev_inst *sdi,
439 struct sr_channel_group *cg;
440 struct channel_group_priv *cgp;
445 for (l = sdi->channel_groups; l != NULL; l = l->next) {
449 hwmon = g_string_sized_new(64);
450 g_string_append_printf(hwmon,
451 "/sys/class/hwmon/hwmon%d/update_interval",
454 if (g_file_test(hwmon->str, G_FILE_TEST_EXISTS)) {
455 fd = g_fopen(hwmon->str, "w");
457 g_string_free(hwmon, TRUE);
461 g_fprintf(fd, "%" PRIu64 "\n", 1000 / samplerate);
465 g_string_free(hwmon, TRUE);
469 SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
472 GString *path = g_string_sized_new(64);
474 int status, ret = SR_OK;
477 status = get_shunt_path(cg, path);
478 if (status != SR_OK) {
483 status = g_file_get_contents(path->str, &contents, NULL, &err);
485 sr_err("Error reading shunt resistance: %s", err->message);
490 *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
493 g_string_free(path, TRUE);
497 SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
499 GString *path = g_string_sized_new(64);;
500 int status, ret = SR_OK;
503 status = get_shunt_path(cg, path);
504 if (status != SR_OK) {
510 * Can't use g_file_set_contents() here, as it calls open() with
511 * O_EXEC flag in a sysfs directory thus failing with EACCES.
513 fd = g_fopen(path->str, "w");
515 sr_err("Error opening %s: %s", path->str, g_strerror(errno));
520 g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
524 g_string_free(path, TRUE);
528 SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
531 struct channel_group_priv *cgp;
536 if (!bl_acme_probe_has_pws(cg)) {
537 sr_err("Probe has no power-switch");
541 val = sr_gpio_getval_export(cgp->pws_gpio);
542 *off = val ? FALSE : TRUE;
547 SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
550 struct channel_group_priv *cgp;
555 if (!bl_acme_probe_has_pws(cg)) {
556 sr_err("Probe has no power-switch");
560 val = sr_gpio_setval_export(cgp->pws_gpio, off ? 0 : 1);
562 sr_err("Error setting power-off state: gpio: %d",
570 static int channel_to_mq(struct sr_channel *ch)
572 struct channel_priv *chp;
576 switch (chp->ch_type) {
580 return SR_MQ_CURRENT;
582 return SR_MQ_VOLTAGE;
583 case TEMP_IN: /* Fallthrough */
585 return SR_MQ_TEMPERATURE;
591 static int channel_to_unit(struct sr_channel *ch)
593 struct channel_priv *chp;
597 switch (chp->ch_type) {
601 return SR_UNIT_AMPERE;
604 case TEMP_IN: /* Fallthrough */
606 return SR_UNIT_CELSIUS;
612 /* We need to scale measurements down from the units used by the drivers. */
613 static float adjust_data(int val, int type)
617 return ((float)val) / 1000000.0;
618 case ENRG_CURR: /* Fallthrough */
619 case ENRG_VOL: /* Fallthrough */
620 case TEMP_IN: /* Fallthrough */
622 return ((float)val) / 1000.0;
628 static float read_sample(struct sr_channel *ch)
630 struct channel_priv *chp;
638 lseek(fd, 0, SEEK_SET);
640 len = read(fd, buf, sizeof(buf));
642 sr_err("Error reading from channel %s (hwmon: %d): %s",
643 ch->name, chp->probe->hwmon_num, g_strerror(errno));
648 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
651 SR_PRIV int bl_acme_open_channel(struct sr_channel *ch)
653 struct channel_priv *chp;
660 switch (chp->ch_type) {
661 case ENRG_PWR: file = "power1_input"; break;
662 case ENRG_CURR: file = "curr1_input"; break;
663 case ENRG_VOL: file = "in1_input"; break;
664 case TEMP_IN: file = "temp1_input"; break;
665 case TEMP_OUT: file = "temp2_input"; break;
667 sr_err("Invalid channel type: %d.", chp->ch_type);
671 snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
672 chp->probe->hwmon_num, file);
674 fd = open(path, O_RDONLY);
676 sr_err("Error opening %s: %s", path, g_strerror(errno));
686 SR_PRIV void bl_acme_close_channel(struct sr_channel *ch)
688 struct channel_priv *chp;
695 SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
697 uint32_t cur_time, elapsed_time;
698 uint64_t nrexpiration;
699 struct sr_datafeed_packet packet, framep;
700 struct sr_datafeed_analog_old analog;
701 struct sr_dev_inst *sdi;
702 struct sr_channel *ch;
703 struct channel_priv *chp;
704 struct dev_context *devc;
719 packet.type = SR_DF_ANALOG_OLD;
720 packet.payload = &analog;
721 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
723 if (read(devc->timer_fd, &nrexpiration, sizeof(nrexpiration)) < 0) {
724 sr_warn("Failed to read timer information");
729 * We were not able to process the previous timer expiration, we are
732 if (nrexpiration > 1)
733 devc->samples_missed += nrexpiration - 1;
736 * XXX This is a nasty workaround...
738 * At high sampling rates and maximum channels we are not able to
739 * acquire samples fast enough, even though frontends still think
740 * that samples arrive on time. This causes shifts in frontend
743 * To compensate for the delay we check if any clock events were
744 * missed and - if so - don't really read the next value, but send
745 * the same sample as fast as possible. We do it until we are back
748 * At high sampling rate this doesn't seem to visibly reduce the
751 for (i = 0; i < nrexpiration; i++) {
752 framep.type = SR_DF_FRAME_BEGIN;
753 sr_session_send(cb_data, &framep);
756 * Due to different units used in each channel we're sending
757 * samples one-by-one.
759 for (chl = sdi->channels; chl; chl = chl->next) {
767 analog.channels = &chonly;
768 analog.num_samples = 1;
769 analog.mq = channel_to_mq(chl->data);
770 analog.unit = channel_to_unit(ch);
773 chp->val = read_sample(ch);
775 analog.data = &chp->val;
776 sr_session_send(cb_data, &packet);
779 framep.type = SR_DF_FRAME_END;
780 sr_session_send(cb_data, &framep);
783 devc->samples_read++;
784 if (devc->limit_samples > 0 &&
785 devc->samples_read >= devc->limit_samples) {
786 sr_info("Requested number of samples reached.");
787 sdi->driver->dev_acquisition_stop(sdi, cb_data);
788 devc->last_sample_fin = g_get_monotonic_time();
790 } else if (devc->limit_msec > 0) {
791 cur_time = g_get_monotonic_time();
792 elapsed_time = cur_time - devc->start_time;
794 if (elapsed_time >= devc->limit_msec) {
795 sr_info("Sampling time limit reached.");
796 sdi->driver->dev_acquisition_stop(sdi, cb_data);
797 devc->last_sample_fin = g_get_monotonic_time();
802 devc->last_sample_fin = g_get_monotonic_time();