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/>.
24 #include <glib/gstdio.h>
28 struct channel_group_priv {
36 struct channel_group_priv *probe;
39 static const uint8_t enrg_i2c_addrs[] = {
40 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47,
43 static const uint8_t temp_i2c_addrs[] = {
44 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b,
47 static const uint32_t pws_gpios[] = {
48 486, 498, 502, 482, 478, 506, 510, 474,
51 static const uint32_t pws_info_gpios[] = {
52 487, 499, 503, 483, 479, 507, 511, 475,
55 #define MOHM_TO_UOHM(x) ((x) * 1000)
56 #define UOHM_TO_MOHM(x) ((x) / 1000)
58 SR_PRIV uint8_t bl_acme_get_enrg_addr(int index)
60 return enrg_i2c_addrs[index];
63 SR_PRIV uint8_t bl_acme_get_temp_addr(int index)
65 return temp_i2c_addrs[index];
68 SR_PRIV gboolean bl_acme_is_sane(void)
73 * We expect sysfs to be present and mounted at /sys, ina226 and
74 * tmp435 sensors detected by the system and their appropriate
75 * drivers loaded and functional.
77 status = g_file_test("/sys", G_FILE_TEST_IS_DIR);
79 sr_err("/sys/ directory not found - sysfs not mounted?");
86 static void probe_name_path(unsigned int addr, GString *path)
89 "/sys/class/i2c-adapter/i2c-1/1-00%02x/name", addr);
93 * For given address fill buf with the path to appropriate hwmon entry.
95 static void probe_hwmon_path(unsigned int addr, GString *path)
98 "/sys/class/i2c-adapter/i2c-1/1-00%02x/hwmon", addr);
101 SR_PRIV gboolean bl_acme_detect_probe(unsigned int addr,
102 int prb_num, const char *prb_name)
104 gboolean ret = FALSE, status;
106 GString *path = g_string_sized_new(64);
110 probe_name_path(addr, path);
111 status = g_file_get_contents(path->str, &buf, &size, &err);
113 sr_dbg("Name for probe %d can't be read: %s",
114 prb_num, err->message);
115 g_string_free(path, TRUE);
119 if (!strncmp(buf, prb_name, strlen(prb_name))) {
121 * Correct driver registered on this address - but is
122 * there an actual probe connected?
124 probe_hwmon_path(addr, path);
125 status = g_file_test(path->str, G_FILE_TEST_IS_DIR);
127 /* We have found an ACME probe. */
133 g_string_free(path, TRUE);
138 static int get_hwmon_index(unsigned int addr)
141 GString *path = g_string_sized_new(64);
145 probe_hwmon_path(addr, path);
146 dir = g_dir_open(path->str, 0, &err);
148 sr_err("Error opening %s: %s", path->str, err->message);
149 g_string_free(path, TRUE);
153 g_string_free(path, TRUE);
156 * The directory should contain a single file named hwmonX where X
157 * is the hwmon index.
159 status = sscanf(g_dir_read_name(dir), "hwmon%d", &hwmon);
162 sr_err("Unable to determine the hwmon entry");
169 static void append_channel(struct sr_dev_inst *sdi, struct sr_channel_group *cg,
172 struct channel_priv *cp;
173 struct dev_context *devc;
174 struct sr_channel *ch;
181 name = g_strdup_printf("P%d_ENRG_PWR", index);
184 name = g_strdup_printf("P%d_ENRG_CURR", index);
187 name = g_strdup_printf("P%d_ENRG_VOL", index);
190 name = g_strdup_printf("P%d_TEMP_IN", index);
193 name = g_strdup_printf("P%d_TEMP_OUT", index);
196 sr_err("Invalid channel type: %d.", type);
200 cp = g_malloc0(sizeof(struct channel_priv));
202 cp->probe = cg->priv;
204 ch = sr_channel_new(devc->num_channels++,
205 SR_CHANNEL_ANALOG, TRUE, name);
209 cg->channels = g_slist_append(cg->channels, ch);
210 sdi->channels = g_slist_append(sdi->channels, ch);
213 SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type,
214 unsigned int addr, int prb_num)
216 struct sr_channel_group *cg;
217 struct channel_group_priv *cgp;
220 /* Obtain the hwmon index. */
221 hwmon = get_hwmon_index(addr);
225 cg = g_malloc0(sizeof(struct sr_channel_group));
226 cgp = g_malloc0(sizeof(struct channel_group_priv));
227 cgp->hwmon_num = hwmon;
228 cgp->probe_type = type;
229 cgp->index = prb_num - 1;
230 cg->name = g_strdup_printf("Probe_%d", prb_num);
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);
241 sr_err("Invalid probe type: %d.", type);
244 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
250 * Sets path to the hwmon attribute if this channel group
251 * supports shunt resistance setting. The caller has to supply
254 static int get_shunt_path(const struct sr_channel_group *cg, GString *path)
256 struct channel_group_priv *cgp;
257 int ret = SR_OK, status;
261 if (cgp->probe_type != PROBE_ENRG) {
262 sr_err("Probe doesn't support shunt resistance setting");
266 g_string_append_printf(path,
267 "/sys/class/hwmon/hwmon%d/shunt_resistor",
271 * The shunt_resistor sysfs attribute is available
272 * in the Linux kernel since version 3.20. We have
273 * to notify the user if this attribute is not present.
275 status = g_file_test(path->str, G_FILE_TEST_EXISTS);
277 sr_err("shunt_resistance attribute not present, please update "
278 "your kernel to version >=3.20");
285 SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg,
288 GString *path = g_string_sized_new(64);
290 int status, ret = SR_OK;
293 status = get_shunt_path(cg, path);
294 if (status != SR_OK) {
299 status = g_file_get_contents(path->str, &contents, NULL, &err);
301 sr_err("Error reading shunt resistance: %s", err->message);
306 *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10));
309 g_string_free(path, TRUE);
313 SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, uint64_t shunt)
315 GString *path = g_string_sized_new(64);;
316 int status, ret = SR_OK;
319 status = get_shunt_path(cg, path);
320 if (status != SR_OK) {
326 * Can't use g_file_set_contents() here, as it calls open() with
327 * O_EXEC flag in a sysfs directory thus failing with EACCES.
329 fd = g_fopen(path->str, "w");
331 sr_err("Error opening %s: %s", path->str, strerror(errno));
332 g_string_free(path, TRUE);
336 g_string_free(path, TRUE);
337 g_fprintf(fd, "%" PRIu64 "\n", MOHM_TO_UOHM(shunt));
339 * XXX There's no g_fclose() in GLib. This seems to work,
345 g_string_free(path, TRUE);
349 SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg,
352 struct channel_group_priv *cgp;
357 val = sr_gpio_getval_export(pws_info_gpios[cgp->index]);
359 sr_err("Probe has no power-switch");
363 val = sr_gpio_getval_export(pws_gpios[cgp->index]);
364 *off = val ? FALSE : TRUE;
369 SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg,
372 struct channel_group_priv *cgp;
377 val = sr_gpio_getval_export(pws_info_gpios[cgp->index]);
379 sr_err("Probe has no power-switch");
383 val = sr_gpio_setval_export(pws_gpios[cgp->index], off ? 0 : 1);
388 static int channel_to_mq(struct sr_channel *ch)
390 struct channel_priv *chp;
394 switch (chp->ch_type) {
398 return SR_MQ_CURRENT;
400 return SR_MQ_VOLTAGE;
401 case TEMP_IN: /* Fallthrough */
403 return SR_MQ_TEMPERATURE;
409 static int channel_to_unit(struct sr_channel *ch)
411 struct channel_priv *chp;
415 switch (chp->ch_type) {
419 return SR_UNIT_AMPERE;
422 case TEMP_IN: /* Fallthrough */
424 return SR_UNIT_CELSIUS;
430 /* We need to scale measurements down from the units used by the drivers. */
431 static float adjust_data(int val, int type)
435 return ((float)val) / 1000000.0;
436 case ENRG_CURR: /* Fallthrough */
437 case ENRG_VOL: /* Fallthrough */
438 case TEMP_IN: /* Fallthrough */
440 return ((float)val) / 1000.0;
446 static float read_sample(struct sr_channel *ch)
448 struct channel_priv *chp;
449 char path[64], *file, buf[16];
455 switch (chp->ch_type) {
456 case ENRG_PWR: file = "power1_input"; break;
457 case ENRG_CURR: file = "curr1_input"; break;
458 case ENRG_VOL: file = "in1_input"; break;
459 case TEMP_IN: file = "temp1_input"; break;
460 case TEMP_OUT: file = "temp2_input"; break;
462 sr_err("Invalid channel type: %d.", chp->ch_type);
466 snprintf(path, sizeof(path), "/sys/class/hwmon/hwmon%d/%s",
467 chp->probe->hwmon_num, file);
468 fd = open(path, O_RDONLY);
470 sr_err("Error opening %s: %s", path, strerror(errno));
475 len = read(fd, buf, sizeof(buf));
478 sr_err("Error reading from %s: %s", path, strerror(errno));
483 return adjust_data(strtol(buf, NULL, 10), chp->ch_type);
486 SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data)
488 uint32_t cur_time, elapsed_time, diff_time;
489 int64_t time_to_sleep;
490 struct sr_datafeed_packet packet, framep;
491 struct sr_datafeed_analog analog;
492 struct sr_dev_inst *sdi;
493 struct sr_channel *ch;
494 struct dev_context *devc;
509 packet.type = SR_DF_ANALOG;
510 packet.payload = &analog;
511 memset(&analog, 0, sizeof(analog));
515 * Reading from sysfs takes some time - try to keep up with samplerate.
517 if (devc->samples_read) {
518 cur_time = g_get_monotonic_time();
519 diff_time = cur_time - devc->last_sample_fin;
520 time_to_sleep = G_USEC_PER_SEC / devc->samplerate - diff_time;
521 if (time_to_sleep > 0)
522 g_usleep(time_to_sleep);
525 framep.type = SR_DF_FRAME_BEGIN;
526 sr_session_send(cb_data, &framep);
529 * Due to different units used in each channel we're sending
530 * samples one-by-one.
532 for (chl = sdi->channels; chl; chl = chl->next) {
538 analog.channels = &chonly;
539 analog.num_samples = 1;
540 analog.mq = channel_to_mq(chl->data);
541 analog.unit = channel_to_unit(ch);
543 valf = read_sample(ch);
545 sr_session_send(cb_data, &packet);
548 framep.type = SR_DF_FRAME_END;
549 sr_session_send(cb_data, &framep);
551 devc->samples_read++;
552 if (devc->limit_samples > 0 &&
553 devc->samples_read >= devc->limit_samples) {
554 sr_info("Requested number of samples reached.");
555 sdi->driver->dev_acquisition_stop(sdi, cb_data);
556 devc->last_sample_fin = g_get_monotonic_time();
558 } else if (devc->limit_msec > 0) {
559 cur_time = g_get_monotonic_time();
560 elapsed_time = cur_time - devc->start_time;
562 if (elapsed_time >= devc->limit_msec) {
563 sr_info("Sampling time limit reached.");
564 sdi->driver->dev_acquisition_stop(sdi, cb_data);
565 devc->last_sample_fin = g_get_monotonic_time();
570 devc->last_sample_fin = g_get_monotonic_time();