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