]>
Commit | Line | Data |
---|---|---|
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 BG |
20 | #include <string.h> |
21 | #include <stdlib.h> /* strtol() */ | |
22 | #include <errno.h> | |
23 | #include <fcntl.h> /* open(), etc... */ | |
61f2b7f7 | 24 | #include <glib/gstdio.h> |
dfaee1de | 25 | #include "protocol.h" |
740ad48a | 26 | #include "gpio.h" |
dfaee1de | 27 | |
6b80b80d BG |
28 | struct channel_group_priv { |
29 | int hwmon_num; | |
61f2b7f7 | 30 | int probe_type; |
740ad48a | 31 | int index; |
6b80b80d BG |
32 | }; |
33 | ||
34 | struct channel_priv { | |
35 | int ch_type; | |
36 | struct channel_group_priv *probe; | |
37 | }; | |
38 | ||
39 | static const uint8_t enrg_i2c_addrs[] = { | |
40 | 0x40, 0x41, 0x44, 0x45, 0x42, 0x43, 0x46, 0x47, | |
41 | }; | |
42 | ||
43 | static const uint8_t temp_i2c_addrs[] = { | |
44 | 0x0, 0x0, 0x0, 0x0, 0x4c, 0x49, 0x4f, 0x4b, | |
45 | }; | |
46 | ||
740ad48a BG |
47 | static const uint32_t pws_gpios[] = { |
48 | 486, 498, 502, 482, 478, 506, 510, 474 | |
49 | }; | |
50 | ||
51 | static 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 |
58 | SR_PRIV uint8_t bl_acme_get_enrg_addr(int index) |
59 | { | |
60 | return enrg_i2c_addrs[index]; | |
61 | } | |
62 | ||
63 | SR_PRIV uint8_t bl_acme_get_temp_addr(int index) | |
64 | { | |
65 | return temp_i2c_addrs[index]; | |
66 | } | |
67 | ||
68 | SR_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 | ||
86 | static 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 | */ | |
95 | static 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 | ||
101 | SR_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; | |
117 | ||
118 | } | |
119 | ||
120 | if (strncmp(buf, prb_name, strlen(prb_name)) == 0) { | |
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 | ||
139 | static 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); | |
148 | if (dir == NULL) { | |
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 | ||
170 | static void append_channel(struct sr_dev_inst *sdi, | |
171 | struct sr_channel_group *cg, | |
172 | int index, int type) | |
173 | { | |
174 | struct channel_priv *cp; | |
175 | struct dev_context *devc; | |
176 | struct sr_channel *ch; | |
177 | char *name; | |
178 | ||
179 | devc = sdi->priv; | |
180 | ||
181 | switch (type) { | |
182 | case ENRG_PWR: | |
183 | name = g_strdup_printf("P%d_ENRG_PWR", index); | |
184 | break; | |
185 | case ENRG_CURR: | |
186 | name = g_strdup_printf("P%d_ENRG_CURR", index); | |
187 | break; | |
188 | case ENRG_VOL: | |
189 | name = g_strdup_printf("P%d_ENRG_VOL", index); | |
190 | break; | |
191 | case TEMP_IN: | |
192 | name = g_strdup_printf("P%d_TEMP_IN", index); | |
193 | break; | |
194 | case TEMP_OUT: | |
195 | name = g_strdup_printf("P%d_TEMP_OUT", index); | |
196 | break; | |
197 | default: | |
198 | sr_err("Bug in the code: invalid channel type!"); | |
199 | return; | |
200 | } | |
201 | ||
202 | cp = g_malloc0(sizeof(struct channel_priv)); | |
203 | cp->ch_type = type; | |
204 | cp->probe = cg->priv; | |
205 | ||
206 | ch = sr_channel_new(devc->num_channels++, | |
207 | SR_CHANNEL_ANALOG, TRUE, name); | |
208 | g_free(name); | |
209 | ||
210 | ch->priv = cp; | |
211 | cg->channels = g_slist_append(cg->channels, ch); | |
212 | sdi->channels = g_slist_append(sdi->channels, ch); | |
213 | } | |
214 | ||
215 | SR_PRIV gboolean bl_acme_register_probe(struct sr_dev_inst *sdi, int type, | |
216 | unsigned int addr, int prb_num) | |
217 | { | |
218 | struct sr_channel_group *cg; | |
219 | struct channel_group_priv *cgp; | |
220 | int hwmon; | |
221 | ||
222 | /* Obtain the hwmon index. */ | |
223 | hwmon = get_hwmon_index(addr); | |
224 | if (hwmon < 0) | |
225 | return FALSE; | |
226 | ||
227 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
228 | cgp = g_malloc0(sizeof(struct channel_group_priv)); | |
229 | cgp->hwmon_num = hwmon; | |
61f2b7f7 | 230 | cgp->probe_type = type; |
740ad48a | 231 | cgp->index = prb_num - 1; |
6b80b80d BG |
232 | cg->name = g_strdup_printf("Probe_%d", prb_num); |
233 | cg->priv = cgp; | |
234 | ||
235 | if (type == PROBE_ENRG) { | |
236 | append_channel(sdi, cg, prb_num, ENRG_PWR); | |
237 | append_channel(sdi, cg, prb_num, ENRG_CURR); | |
238 | append_channel(sdi, cg, prb_num, ENRG_VOL); | |
239 | } else if (type == PROBE_TEMP) { | |
240 | append_channel(sdi, cg, prb_num, TEMP_IN); | |
241 | append_channel(sdi, cg, prb_num, TEMP_OUT); | |
242 | } else { | |
243 | sr_err("Bug in the code: invalid probe type!"); | |
244 | } | |
245 | ||
246 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
247 | ||
248 | return TRUE; | |
249 | } | |
250 | ||
61f2b7f7 BG |
251 | /* |
252 | * Sets path to the hwmon attribute if this channel group | |
253 | * supports shunt resistance setting. The caller has to supply | |
254 | * a valid GString. | |
255 | */ | |
256 | static int get_shunt_path(const struct sr_channel_group *cg, GString *path) | |
257 | { | |
258 | struct channel_group_priv *cgp; | |
259 | int ret = SR_OK, status; | |
260 | ||
261 | cgp = cg->priv; | |
262 | ||
263 | if (cgp->probe_type != PROBE_ENRG) { | |
264 | sr_err("Probe doesn't support shunt resistance setting"); | |
265 | return SR_ERR_ARG; | |
266 | } | |
267 | ||
268 | g_string_append_printf(path, | |
269 | "/sys/class/hwmon/hwmon%d/shunt_resistor", | |
270 | cgp->hwmon_num); | |
271 | ||
272 | /* | |
273 | * The shunt_resistor sysfs attribute is available | |
274 | * in the Linux kernel since version 3.20. We have | |
275 | * to notify the user if this attribute is not | |
276 | * present. | |
277 | */ | |
278 | status = g_file_test(path->str, G_FILE_TEST_EXISTS); | |
279 | if (!status) { | |
280 | sr_err("shunt_resistance attribute not present please update " | |
281 | "your kernel to version >=3.20"); | |
282 | return SR_ERR_NA; | |
283 | } | |
284 | ||
285 | return ret; | |
286 | } | |
287 | ||
288 | SR_PRIV int bl_acme_get_shunt(const struct sr_channel_group *cg, | |
289 | uint64_t *shunt) | |
290 | { | |
291 | GString *path = g_string_sized_new(64); | |
292 | gchar *contents; | |
293 | int status, ret = SR_OK; | |
294 | GError *err = NULL; | |
295 | ||
296 | status = get_shunt_path(cg, path); | |
297 | if (status != SR_OK) { | |
298 | ret = status; | |
299 | goto out; | |
300 | } | |
301 | ||
302 | status = g_file_get_contents(path->str, &contents, NULL, &err); | |
303 | if (!status) { | |
304 | sr_err("Error reading shunt resistance: %s", err->message); | |
305 | ret = SR_ERR_IO; | |
306 | goto out; | |
307 | } | |
308 | ||
309 | *shunt = UOHM_TO_MOHM(strtol(contents, NULL, 10)); | |
310 | ||
311 | out: | |
312 | g_string_free(path, TRUE); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | SR_PRIV int bl_acme_set_shunt(const struct sr_channel_group *cg, | |
317 | uint64_t shunt) | |
318 | { | |
319 | GString *path = g_string_sized_new(64);; | |
320 | int status, ret = SR_OK; | |
321 | FILE *fd; | |
322 | ||
323 | status = get_shunt_path(cg, path); | |
324 | if (status != SR_OK) { | |
325 | ret = status; | |
326 | goto out; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Can't use g_file_set_contents() here, as it calls open() with | |
331 | * O_EXEC flag in a sysfs directory thus failing with EACCES. | |
332 | */ | |
333 | fd = g_fopen(path->str, "w"); | |
334 | if (!fd) { | |
335 | sr_err("Error opening %s: %s", path->str, strerror(errno)); | |
336 | g_string_free(path, TRUE); | |
337 | return SR_ERR_IO; | |
338 | } | |
339 | ||
340 | g_string_free(path, TRUE); | |
341 | g_fprintf(fd, "%llu\n", MOHM_TO_UOHM(shunt)); | |
342 | /* | |
343 | * XXX There's no g_fclose() in GLib. This seems to work, | |
344 | * but is it safe? | |
345 | */ | |
346 | fclose(fd); | |
347 | ||
348 | out: | |
349 | g_string_free(path, TRUE); | |
350 | return ret; | |
351 | } | |
352 | ||
740ad48a BG |
353 | SR_PRIV int bl_acme_read_power_state(const struct sr_channel_group *cg, |
354 | gboolean *off) | |
355 | { | |
356 | struct channel_group_priv *cgp; | |
357 | int val; | |
358 | ||
359 | cgp = cg->priv; | |
360 | ||
361 | val = sr_gpio_getval_export(pws_info_gpios[cgp->index]); | |
362 | if (val != 1) { | |
363 | sr_err("Probe has no power-switch"); | |
364 | return SR_ERR_ARG; | |
365 | } | |
366 | ||
367 | val = sr_gpio_getval_export(pws_gpios[cgp->index]); | |
368 | *off = val ? FALSE : TRUE; | |
369 | ||
370 | return SR_OK; | |
371 | } | |
372 | ||
373 | SR_PRIV int bl_acme_set_power_off(const struct sr_channel_group *cg, | |
374 | gboolean off) | |
375 | { | |
376 | struct channel_group_priv *cgp; | |
377 | int val; | |
378 | ||
379 | cgp = cg->priv; | |
380 | ||
381 | val = sr_gpio_getval_export(pws_info_gpios[cgp->index]); | |
382 | if (val != 1) { | |
383 | sr_err("Probe has no power-switch"); | |
384 | return SR_ERR_ARG; | |
385 | } | |
386 | ||
387 | val = sr_gpio_setval_export(pws_gpios[cgp->index], off ? 0 : 1); | |
388 | ||
389 | return SR_OK; | |
390 | } | |
391 | ||
6b80b80d | 392 | static int channel_to_mq(struct sr_channel *ch) |
dfaee1de | 393 | { |
6b80b80d BG |
394 | struct channel_priv *chp; |
395 | ||
396 | chp = ch->priv; | |
397 | ||
398 | switch (chp->ch_type) { | |
399 | case ENRG_PWR: | |
400 | return SR_MQ_POWER; | |
401 | case ENRG_CURR: | |
402 | return SR_MQ_CURRENT; | |
403 | case ENRG_VOL: | |
404 | return SR_MQ_VOLTAGE; | |
405 | case TEMP_IN: | |
406 | case TEMP_OUT: | |
407 | return SR_MQ_TEMPERATURE; | |
408 | default: | |
409 | return -1; | |
410 | } | |
411 | } | |
412 | ||
413 | static int channel_to_unit(struct sr_channel *ch) | |
414 | { | |
415 | struct channel_priv *chp; | |
416 | ||
417 | chp = ch->priv; | |
418 | ||
419 | switch (chp->ch_type) { | |
420 | case ENRG_PWR: | |
421 | return SR_UNIT_WATT; | |
422 | case ENRG_CURR: | |
423 | return SR_UNIT_AMPERE; | |
424 | case ENRG_VOL: | |
425 | return SR_UNIT_VOLT; | |
426 | case TEMP_IN: | |
427 | case TEMP_OUT: | |
428 | return SR_UNIT_CELSIUS; | |
429 | default: | |
430 | return -1; | |
431 | } | |
432 | } | |
433 | ||
434 | /* We need to scale measurements down from the units used by the drivers. */ | |
435 | static float adjust_data(int val, int type) | |
436 | { | |
437 | switch (type) { | |
438 | case ENRG_PWR: | |
439 | return ((float)val) / 1000000.0; | |
440 | case ENRG_CURR: | |
441 | case ENRG_VOL: | |
442 | case TEMP_IN: | |
443 | case TEMP_OUT: | |
444 | return ((float)val) / 1000.0; | |
445 | default: | |
446 | return 0.0; | |
447 | } | |
448 | } | |
449 | ||
450 | static float read_sample(struct sr_channel *ch) | |
451 | { | |
452 | struct channel_priv *chp; | |
453 | char path[64], *file, buf[16]; | |
454 | ssize_t len; | |
455 | int fd; | |
456 | ||
457 | chp = ch->priv; | |
458 | ||
459 | switch (chp->ch_type) { | |
460 | case ENRG_PWR: file = "power1_input"; break; | |
461 | case ENRG_CURR: file = "curr1_input"; break; | |
462 | case ENRG_VOL: file = "in1_input"; break; | |
463 | case TEMP_IN: file = "temp1_input"; break; | |
464 | case TEMP_OUT: file = "temp2_input"; break; | |
465 | default: | |
466 | sr_err("Bug in the code: invalid channel type!"); | |
467 | return -1.0; | |
468 | } | |
469 | ||
470 | snprintf(path, sizeof(path), | |
471 | "/sys/class/hwmon/hwmon%d/%s", | |
472 | chp->probe->hwmon_num, file); | |
473 | fd = open(path, O_RDONLY); | |
474 | if (fd < 0) { | |
475 | sr_err("Error opening %s: %s", path, strerror(errno)); | |
476 | ch->enabled = FALSE; | |
477 | return -1.0; | |
478 | } | |
479 | ||
480 | len = read(fd, buf, sizeof(buf)); | |
481 | close(fd); | |
482 | if (len < 0) { | |
483 | sr_err("error reading from %s: %s", path, strerror(errno)); | |
484 | ch->enabled = FALSE; | |
485 | return -1.0; | |
486 | } | |
487 | ||
488 | return adjust_data(strtol(buf, NULL, 10), chp->ch_type); | |
489 | } | |
490 | ||
491 | SR_PRIV int bl_acme_receive_data(int fd, int revents, void *cb_data) | |
492 | { | |
493 | uint32_t cur_time, elapsed_time, diff_time; | |
494 | int64_t time_to_sleep; | |
495 | struct sr_datafeed_packet packet, framep; | |
496 | struct sr_datafeed_analog analog; | |
497 | struct sr_dev_inst *sdi; | |
498 | struct sr_channel *ch; | |
dfaee1de | 499 | struct dev_context *devc; |
6b80b80d BG |
500 | GSList *chl, chonly; |
501 | float valf; | |
dfaee1de BG |
502 | |
503 | (void)fd; | |
6b80b80d BG |
504 | (void)revents; |
505 | ||
506 | sdi = cb_data; | |
507 | if (!sdi) | |
508 | return TRUE; | |
dfaee1de | 509 | |
6b80b80d BG |
510 | devc = sdi->priv; |
511 | if (!devc) | |
dfaee1de BG |
512 | return TRUE; |
513 | ||
6b80b80d BG |
514 | packet.type = SR_DF_ANALOG; |
515 | packet.payload = &analog; | |
516 | memset(&analog, 0, sizeof(analog)); | |
517 | analog.data = &valf; | |
518 | ||
519 | /* | |
520 | * Reading from sysfs takes some time - try to | |
521 | * keep up with samplerate. | |
522 | */ | |
523 | if (devc->samples_read) { | |
524 | cur_time = g_get_monotonic_time(); | |
525 | diff_time = cur_time - devc->last_sample_fin; | |
526 | time_to_sleep = G_USEC_PER_SEC / devc->samplerate - diff_time; | |
527 | if (time_to_sleep > 0) | |
528 | g_usleep(time_to_sleep); | |
529 | } | |
530 | ||
531 | framep.type = SR_DF_FRAME_BEGIN; | |
532 | sr_session_send(cb_data, &framep); | |
533 | ||
534 | /* | |
535 | * Due to different units used in each channel we're sending | |
536 | * samples one-by-one. | |
537 | */ | |
538 | for (chl = sdi->channels; chl; chl = chl->next) { | |
539 | ch = chl->data; | |
540 | if (!ch->enabled) | |
541 | continue; | |
542 | chonly.next = NULL; | |
543 | chonly.data = ch; | |
544 | analog.channels = &chonly; | |
545 | analog.num_samples = 1; | |
546 | analog.mq = channel_to_mq(chl->data); | |
547 | analog.unit = channel_to_unit(ch); | |
548 | ||
549 | valf = read_sample(ch); | |
550 | ||
551 | sr_session_send(cb_data, &packet); | |
552 | } | |
553 | ||
554 | framep.type = SR_DF_FRAME_END; | |
555 | sr_session_send(cb_data, &framep); | |
556 | ||
557 | devc->samples_read++; | |
558 | if (devc->limit_samples > 0 && | |
559 | devc->samples_read >= devc->limit_samples) { | |
560 | sr_info("Requested number of samples reached."); | |
561 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
562 | devc->last_sample_fin = g_get_monotonic_time(); | |
dfaee1de | 563 | return TRUE; |
6b80b80d BG |
564 | } else if (devc->limit_msec > 0) { |
565 | cur_time = g_get_monotonic_time(); | |
566 | elapsed_time = cur_time - devc->start_time; | |
dfaee1de | 567 | |
6b80b80d BG |
568 | if (elapsed_time >= devc->limit_msec) { |
569 | sr_info("Sampling time limit reached."); | |
570 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
571 | devc->last_sample_fin = g_get_monotonic_time(); | |
572 | return TRUE; | |
573 | } | |
dfaee1de BG |
574 | } |
575 | ||
6b80b80d | 576 | devc->last_sample_fin = g_get_monotonic_time(); |
dfaee1de BG |
577 | return TRUE; |
578 | } |