1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
#define NXP_GPIO(BANK, IO) (((( BANK - 1 ) * 32) + IO))
/* Used for both mtcap3 0.0 and 0.1 */
static struct gpio_pin gpio_pins_mtcap3_0_0[] = {
{
.name = "ETH_RESET",
.pin = {
.gpio = ~0U, // ETH_NRST - has ext PU
.flags = GPIOD_OUT_HIGH,
.label = "eth-reset",
},
// mts-io in init leaves eth-reset asserted even though it is really
// an nrst pin, and does this across boards. This is wrong logically,
// but active_low must be 0 as legacy and for this to function properly.
.active_low = 0,
.do_gpio_desc = 1
},
{
.name = "RADIO_POWER_MONITOR",
.pin = {
.gpio = ~0U, // CELL_STATUS
.flags = GPIOD_IN,
.label = "radio-power-monitor",
},
.do_gpio_desc = 1
},
{
.name = "RADIO_RESET",
.pin = {
.gpio = ~0U, // CELL_RESET - has ext PD - inverted through FET
.flags = GPIOD_OUT_LOW,
.label = "radio-reset",
},
.do_gpio_desc = 1
},
{
.name = "RADIO_ONOFF",
.pin = {
.gpio = ~0U, // CELL_ONOFF - has ext PD - inverted through FET
.flags = GPIOD_OUT_HIGH,
.label = "radio-onoff",
},
.do_gpio_desc = 1
},
{
.name = "RADIO_POWER",
.pin = {
.gpio = ~0U, // CELL_PWR_EN - has ext PD
.flags = GPIOD_OUT_HIGH,
.label = "radio-power",
},
.do_gpio_desc = 1
},
{
.name = "RADIO_NETLIGHT", // LED4 is for cell network status
.pin = {
.gpio = ~0U,
.flags = GPIOD_IN,
.label = "radio-netlight",
},
.do_gpio_desc = 1
},
{
.name = "DEVICE_RESET",
.pin = {
.gpio = ~0U, // SWITCH_IN - has EXT PU
.flags = GPIOD_IN,
.label = "reset",
},
.active_low = 1,
.do_gpio_desc = 1
},
{
.name = "LORA_RESET",
.pin = {
.gpio = ~0U, // LORA_RST
.flags = GPIOD_OUT_LOW,
.label = "lora/reset",
},
.do_gpio_desc = 1,
.capability = CAPA_LORA,
.active_low = 1,
},
{
.name = "LORA_LBT_RESET",
.pin = {
.gpio = ~0U, // LORA_LBT_nRESET
.flags = GPIOD_OUT_LOW,
.label = "lora/lbt-reset",
},
.do_gpio_desc = 1,
.capability = CAPA_LORA_LBT,
.active_low = 1,
},
/* LEDs */
{
.name = "STATUS_LED",
.pin = {
.gpio = ~0U,
.flags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
.label = "led-status",
},
.do_gpio_desc = 1
},
{
.name = "LED2",
.pin = {
.gpio = ~0U,
.flags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
.label = "led-lora",
},
.do_gpio_desc = 1
},
{
.name = "LED3",
.pin = {
.gpio = ~0U,
.flags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
.label = "led-cell",
},
.do_gpio_desc = 1
},
{
.name = "LED4",
.pin = {
.gpio = ~0U,
.flags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE,
.label = "led-power",
},
.do_gpio_desc = 1
},
{ },
};
// Note on EG95 reset logic: this code was adapted from radio.c in
// RadioSupervisoryChips/STM8L101-MTQ.
// EG95 timings
#define EG95_PWRKEY_KEEPOUT_WAIT_MS (60)
#define EG95_PWRKEY_LOW_ON_WAIT_MS (540) // >= 500ms in datasheet
#define EG95_PWRKEY_LOW_OFF_WAIT_MS (700) // >650ms in datasheet
#define EG95_POWER_MON_ON_WAIT_S (30)
#define EG95_POWER_MON_OFF_WAIT_S (35)
#define EG95_POWER_KEEPOUT_WAIT_MS (60)
#define EG95_RESET_N_WAIT_MS (300)
/* radio control (power/reset) for mtcap3 */
static int radio_off_mtcap3(void)
{
int i = 0;
int value = 0;
struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-onoff");
struct gpio_pin *power_pin = gpio_pin_by_attr_name("radio-power");
if (!onoff_pin || !pwrmon_pin) {
return -ENODEV;
}
value = gpiod_get_value(pwrmon_pin->desc);
if(value == 0) {
log_warning("cell radio is already off");
return 0;
}
/* The reference manual indicates that PWRKEY is equivalent to AT+QPOWD */
log_info("turning off cell radio");
// Toggle PWRKEY - logic is reversed to the module through the FET
gpiod_set_value(onoff_pin->desc, 0);
msleep(EG95_PWRKEY_KEEPOUT_WAIT_MS);
gpiod_set_value(onoff_pin->desc, 1);
msleep(EG95_PWRKEY_LOW_OFF_WAIT_MS);
gpiod_set_value(onoff_pin->desc, 0);
// Wait for module to indicate status
for (i=0; i<=EG95_POWER_MON_OFF_WAIT_S; i++) {
value = gpiod_get_value(pwrmon_pin->desc);
if(!value) {
break;
}
msleep(1000);
}
// disable power to the radio; We want to do this generally for
// battery powered MTCAP based devices.
gpiod_set_value(power_pin->desc, 0);
msleep(EG95_POWER_KEEPOUT_WAIT_MS);
if(value != 0) {
log_warning("cell radio was still on.");
} else {
log_info("cell radio has been turned off");
}
return 0;
}
static int radio_on_mtcap3(void)
{
int i = 0;
int value = 0;
struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-onoff");
struct gpio_pin *power_pin = gpio_pin_by_attr_name("radio-power");
if (!onoff_pin || !pwrmon_pin || !power_pin) {
return -ENODEV;
}
value = gpiod_get_value(pwrmon_pin->desc);
if(value != 0) {
log_warning("cell radio is already on");
return 0;
}
log_info("turning on cell radio");
// Toggle PWRKEY - lgoic is reversed to the module through the FET
gpiod_set_value(onoff_pin->desc, 0);
msleep(EG95_PWRKEY_KEEPOUT_WAIT_MS);
// Enable power to the radio
gpiod_set_value(power_pin->desc, 1);
msleep(EG95_POWER_KEEPOUT_WAIT_MS);
gpiod_set_value(onoff_pin->desc, 1);
msleep(EG95_PWRKEY_LOW_ON_WAIT_MS);
// Wait for module to inidcate status.
// The spec says >= 10s, but MTQ code does 30; will be on the order of secs
for(i=0; i<=EG95_POWER_MON_ON_WAIT_S; i++) {
value = gpiod_get_value(pwrmon_pin->desc);
if(value) {
break;
}
msleep(1000);
}
// Set pwrkey high (through the FET switch);
// MTQ code does this after waiting for radio-power-monitor assert
gpiod_set_value(onoff_pin->desc, 0);
if(value == 0) {
log_warning("cell radio is still off.");
} else {
log_info("cell radio has been turned on");
}
return 0;
}
static ssize_t mts_attr_store_radio_power_mtcap3(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count) {
int value;
int err;
if (sscanf(buf, "%i", &value) != 1) {
return -EINVAL;
}
mutex_lock(&mts_io_mutex);
if (value == 0) {
err = radio_off_mtcap3();
} else if (value == 1) {
err = radio_on_mtcap3();
} else {
err = -EINVAL;
}
mutex_unlock(&mts_io_mutex);
if (err) {
return err;
}
return count;
}
// *_radio_enable_* is here for backward compatibility
static ssize_t mts_attr_store_radio_enable_mtcap3(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count) {
// Included for legacy compatibilty. The 'radio-power' object controls
// power to the module, rather than 'enable.' The radio is always
// enabled, and 'radio-power' mirrors the state of the regulator enable,
// alleviating confusion, as well as ensuring clean shut down which
// reduce module failures.
return count;
}
static ssize_t mts_attr_show_radio_enable_mtcap3(struct device *dev,
struct device_attribute *attr,
char *buf) {
return sprintf(buf, "%d\n", 1);
}
static ssize_t mts_attr_store_radio_reset_mtcap3(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count) {
int i;
int value;
struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
struct gpio_pin *reset_pin = gpio_pin_by_attr_name("radio-reset");
if ( !pwrmon_pin || !reset_pin) {
return -ENODEV;
}
value = gpiod_get_value(pwrmon_pin->desc);
if (sscanf(buf, "%i", &value) != 1) {
return -EINVAL;
}
if (value != 0 && value != -1) {
return -EINVAL;
}
reset_radio_udev_discovery();
mutex_lock(&mts_io_mutex);
radio_off_mtcap3();
radio_on_mtcap3();
value = gpiod_get_value(pwrmon_pin->desc);
// Ensure that the module status indicates that it is up
if(!value) {
// Something has gone wrong
log_warning("cell radio not responding. Applying hard reset.");
// The manual advises against doing this as it is disorderly
// We do it here as a last resort.
gpiod_set_value(reset_pin->desc, 1);
msleep(EG95_RESET_N_WAIT_MS);
gpiod_set_value(reset_pin->desc, 0);
for(i=0; i<=EG95_POWER_MON_ON_WAIT_S; i++) {
value = gpiod_get_value(pwrmon_pin->desc);
if(value) {
break;
}
msleep(1000);
}
if(value == 0) {
log_warning("Unable to reset radio.");
} else {
log_info("cell radio has been reset");
}
} else {
log_info("cell radio has been reset");
}
mutex_unlock(&mts_io_mutex);
return count;
}
static ssize_t mts_attr_show_radio_power_mtcap3(struct device *dev,
struct device_attribute *attr,
char *buf) {
int value;
struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor");
if ( !pwrmon_pin) {
return -ENODEV;
}
value = gpiod_get_value(pwrmon_pin->desc);
return sprintf(buf, "%d\n", value);
}
static DEVICE_ATTR_MTS(dev_attr_radio_reset_mtcap3, "radio-reset",
mts_attr_show_gpio_pin, mts_attr_store_radio_reset_mtcap3);
static DEVICE_ATTR_MTS(dev_attr_radio_power_mtcap3, "radio-power",
mts_attr_show_radio_power_mtcap3, mts_attr_store_radio_power_mtcap3);
static DEVICE_ATTR_RO_MTS(dev_attr_radio_power_monitor_mtcap3, "radio-power-monitor",
mts_attr_show_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_eth_reset_mtcap3, "eth-reset",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_lora_gpio_mtcap3, "led-lora",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_led_cell_gpio_mtcap3, "led-cell",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_radio_enable_mtcap3, "radio-enable",
mts_attr_show_radio_enable_mtcap3, mts_attr_store_radio_enable_mtcap3);
static DEVICE_ATTR_MTS(dev_attr_led_power_gpio_mtcap3, "led-power",
mts_attr_show_gpio_pin, mts_attr_store_gpio_pin);
static struct attribute *mtcap3_0_0_platform_attributes[] = {
&dev_attr_vendor_id.attr,
&dev_attr_product_id.attr,
&dev_attr_device_id.attr,
&dev_attr_uuid.attr,
&dev_attr_hw_version.attr,
&dev_attr_imei.attr,
&dev_attr_eth_mac.attr,
&dev_attr_has_radio.attr,
&dev_attr_reset.attr,
&dev_attr_reset_monitor.attr,
&dev_attr_reset_monitor_intervals.attr,
&dev_attr_eth_reset_mtcap3.attr,
&dev_attr_led_status.attr,
&dev_attr_led_cell_gpio_mtcap3.attr,
&dev_attr_led_lora_gpio_mtcap3.attr,
&dev_attr_led_power_gpio_mtcap3.attr,
/* Set to NULL if no radio -- radio_power should be 1st radio attribute */
&dev_attr_radio_power_mtcap3.attr,
&dev_attr_radio_reset_mtcap3.attr,
&dev_attr_radio_power_monitor_mtcap3.attr,
&dev_attr_radio_reset_backoffs.attr,
&dev_attr_radio_reset_backoff_index.attr,
&dev_attr_radio_reset_backoff_seconds.attr,
&dev_attr_radio_enable_mtcap3.attr,
// UDEV notification of radio discovery
&dev_attr_radio_udev_discovery.attr,
&dev_attr_radio_reset_monitor.attr,
NULL,
};
static int
is_radio_power_attr_mtcap3(struct attribute *attr) {
return (attr == &dev_attr_radio_power_mtcap3.attr);
}
static struct attribute_group mtcap3_0_0_platform_attribute_group = {
.attrs = mtcap3_0_0_platform_attributes
};
//
// on-board LORA attributes are to be stored in the lora/ sub-directory
//
//
static DEVICE_ATTR_MTS(dev_attr_lora_reset_mtcap3, "reset",
mts_attr_show_lora_gpio_pin, mts_attr_store_lora_gpio_pin);
static DEVICE_ATTR_MTS(dev_attr_lora_lbt_reset_mtcap3, "lbt-reset",
mts_attr_show_lora_gpio_pin, mts_attr_store_lora_gpio_pin);
static DEVICE_ATTR_RO_MTS(dev_attr_lora_eui_mtcap3, "eui",
mts_attr_show_lora_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_lora_product_id_mtcap3, "product-id",
mts_attr_show_lora_product_info);
static DEVICE_ATTR_RO_MTS(dev_attr_lora_hw_version_mtcap3, "hw-version",
mts_attr_show_lora_product_info);
static struct attribute *mtcap3_0_0_lora_attributes[] = {
&dev_attr_lora_eui_mtcap3.attr,
&dev_attr_lora_product_id_mtcap3.attr,
&dev_attr_lora_hw_version_mtcap3.attr,
&dev_attr_lora_reset_mtcap3.attr,
NULL,
};
static struct attribute_group mtcap3_0_0_lora_attribute_group = {
.attrs = mtcap3_0_0_lora_attributes
};
static struct attribute *mtcap3_0_0_lora_lbt_attributes[] = {
&dev_attr_lora_lbt_reset_mtcap3.attr,
};
|