summaryrefslogtreecommitdiff
path: root/io-module/telit_radio.c
blob: fbac4fa1d01c332cca6983926102844c47501109 (plain)
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

static int radio_off_telit(void)
{
	int value, ret;
	struct gpio_pin *pwrmon_pin = gpio_pin_by_name("PWRMON");
	struct gpio_pin *onoff_pin = gpio_pin_by_name("3G_ONOFF");
	struct gpio_pin *rst_pin = gpio_pin_by_name("3G_RST");

	if (!onoff_pin || !pwrmon_pin || !rst_pin) {
		return -ENODEV;
	}

	value = gpio_get_value(pwrmon_pin->pin.gpio);
	if(value == 0) {
		log_error("radio is already off");
		return -EINVAL;
	}

	// drive on/off pin low for at least 3 sec
	log_info("shutting down radio");
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 0, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	msleep(3500);

	// set on/off pin high
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 1, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	// wait for radio to power off
	msleep(5000);

	// check that power is low
	value = gpio_get_value(pwrmon_pin->pin.gpio);
	if(value != 0) {
		log_warning("radio is still on. performing radio reset.");
		//Perform Hard Reset
		ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 0, rst_pin->pin.flags & GPIOF_PULLUP);
		if (ret) {
			return ret;
		}

		msleep(500);

		// set pin high
		ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 1, rst_pin->pin.flags & GPIOF_PULLUP);
		if (ret) {
			return ret;
		}
	} else {
		log_info("radio has been shut down");
	}

	return ret;
}

static int radio_on_telit(void)
{
	int value, ret;
	struct gpio_pin *pwrmon_pin = gpio_pin_by_name("PWRMON");
	struct gpio_pin *onoff_pin = gpio_pin_by_name("3G_ONOFF");
	struct gpio_pin *rst_pin = gpio_pin_by_name("3G_RST");

	if (!onoff_pin || !pwrmon_pin || !rst_pin) {
		return -ENODEV;
	}

	value = gpio_get_value(pwrmon_pin->pin.gpio);
	if(value != 0) {
		log_error("radio is already on");
		return -EINVAL;
	}

	// drive on/off pin low for at least 5 sec
	log_info("turning on radio");
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 0, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	msleep(5500);

	// set on/off pin high
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 1, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	msleep(200);

	// check that power is high
	value = gpio_get_value(pwrmon_pin->pin.gpio);
	if(value == 0) {
		log_warning("radio is still off. performing radio reset");
		//Perform Hard Reset
		ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 0, rst_pin->pin.flags & GPIOF_PULLUP);
		if (ret) {
			return ret;
		}

		msleep(500);

		// set pin high
		ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 1, rst_pin->pin.flags & GPIOF_PULLUP);
		if (ret) {
			return ret;
		}
	} else {
		log_info("radio has been turned on");
	}

	return ret;
}

static ssize_t mts_attr_store_radio_power_telit(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;
	}
	if (value == 0) {
		mutex_lock(&mts_io_mutex);
		err = radio_off_telit();
		mutex_unlock(&mts_io_mutex);
	} else {
		mutex_lock(&mts_io_mutex);
		err = radio_on_telit();
		mutex_unlock(&mts_io_mutex);
	}

	if (err) {
		return err;
	}

	return count;
}

static int radio_reset_telit(void)
{
	int ret;
	struct gpio_pin *rst_pin = gpio_pin_by_name("3G_RST");
	struct gpio_pin *onoff_pin = gpio_pin_by_name("3G_ONOFF");

	if (!rst_pin || !onoff_pin) {
		return -ENODEV;
	}

	// drive reset pin low for 500ms
	ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 0, rst_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	msleep(500);

	// set pin high
	ret = gpio_direction_output_pullup(rst_pin->pin.gpio, 1, rst_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	// wait for 2 sec before toggling on/off pin
	msleep(2000);

	// drive on/off pin low for 6 sec
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 0, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	msleep(6000);

	// set on/off pin high
	ret = gpio_direction_output_pullup(onoff_pin->pin.gpio, 1, onoff_pin->pin.flags & GPIOF_PULLUP);
	if (ret) {
		return ret;
	}

	return ret;
}

static ssize_t mts_attr_store_radio_reset_telit(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;
	}
	if (value != 0) {
		return -EINVAL;
	}

	mutex_lock(&mts_io_mutex);

	err = radio_reset_telit();

	mutex_unlock(&mts_io_mutex);

	if (err) {
		return err;
	}

	return count;
}

static DEVICE_ATTR_MTS(dev_attr_radio_power_telit, "radio-power",
	mts_attr_show_gpio_pin, mts_attr_store_radio_power_telit);

static DEVICE_ATTR_MTS(dev_attr_radio_reset_telit, "radio-reset",
	mts_attr_show_gpio_pin, mts_attr_store_radio_reset_telit);