1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/iio/iio.h>
10#include <linux/device.h>
11#include <linux/delay.h>
12
13#include "ms_sensors_i2c.h"
14
15
16static const u16 ms_sensors_ht_t_conversion_time[] = { 50000, 25000,
17 13000, 7000 };
18static const u16 ms_sensors_ht_h_conversion_time[] = { 16000, 3000,
19 5000, 8000 };
20static const u16 ms_sensors_tp_conversion_time[] = { 500, 1100, 2100,
21 4100, 8220, 16440 };
22
23#define MS_SENSORS_SERIAL_READ_MSB 0xFA0F
24#define MS_SENSORS_SERIAL_READ_LSB 0xFCC9
25#define MS_SENSORS_CONFIG_REG_WRITE 0xE6
26#define MS_SENSORS_CONFIG_REG_READ 0xE7
27#define MS_SENSORS_HT_T_CONVERSION_START 0xF3
28#define MS_SENSORS_HT_H_CONVERSION_START 0xF5
29
30#define MS_SENSORS_TP_PROM_READ 0xA0
31#define MS_SENSORS_TP_T_CONVERSION_START 0x50
32#define MS_SENSORS_TP_P_CONVERSION_START 0x40
33#define MS_SENSORS_TP_ADC_READ 0x00
34
35#define MS_SENSORS_NO_READ_CMD 0xFF
36
37
38
39
40
41
42
43
44
45
46
47int ms_sensors_reset(void *cli, u8 cmd, unsigned int delay)
48{
49 int ret;
50 struct i2c_client *client = cli;
51
52 ret = i2c_smbus_write_byte(client, cmd);
53 if (ret) {
54 dev_err(&client->dev, "Failed to reset device\n");
55 return ret;
56 }
57 usleep_range(delay, delay + 1000);
58
59 return 0;
60}
61EXPORT_SYMBOL(ms_sensors_reset);
62
63
64
65
66
67
68
69
70
71
72
73int ms_sensors_read_prom_word(void *cli, int cmd, u16 *word)
74{
75 int ret;
76 struct i2c_client *client = cli;
77
78 ret = i2c_smbus_read_word_swapped(client, cmd);
79 if (ret < 0) {
80 dev_err(&client->dev, "Failed to read prom word\n");
81 return ret;
82 }
83 *word = ret;
84
85 return 0;
86}
87EXPORT_SYMBOL(ms_sensors_read_prom_word);
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104int ms_sensors_convert_and_read(void *cli, u8 conv, u8 rd,
105 unsigned int delay, u32 *adc)
106{
107 int ret;
108 __be32 buf = 0;
109 struct i2c_client *client = cli;
110
111
112 ret = i2c_smbus_write_byte(client, conv);
113 if (ret)
114 goto err;
115 usleep_range(delay, delay + 1000);
116
117
118 if (rd != MS_SENSORS_NO_READ_CMD)
119 ret = i2c_smbus_read_i2c_block_data(client, rd, 3, (u8 *)&buf);
120 else
121 ret = i2c_master_recv(client, (u8 *)&buf, 3);
122 if (ret < 0)
123 goto err;
124
125 dev_dbg(&client->dev, "ADC raw value : %x\n", be32_to_cpu(buf) >> 8);
126 *adc = be32_to_cpu(buf) >> 8;
127
128 return 0;
129err:
130 dev_err(&client->dev, "Unable to make sensor adc conversion\n");
131 return ret;
132}
133EXPORT_SYMBOL(ms_sensors_convert_and_read);
134
135
136
137
138
139
140
141
142
143
144
145
146static bool ms_sensors_crc_valid(u32 value)
147{
148 u32 polynom = 0x988000;
149 u32 msb = 0x800000;
150 u32 mask = 0xFF8000;
151 u32 result = value & 0xFFFF00;
152 u8 crc = value & 0xFF;
153
154 while (msb != 0x80) {
155 if (result & msb)
156 result = ((result ^ polynom) & mask)
157 | (result & ~mask);
158 msb >>= 1;
159 mask >>= 1;
160 polynom >>= 1;
161 }
162
163 return result == crc;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185int ms_sensors_read_serial(struct i2c_client *client, u64 *sn)
186{
187 u8 i;
188 __be64 rcv_buf = 0;
189 u64 rcv_val;
190 __be16 send_buf;
191 int ret;
192
193 struct i2c_msg msg[2] = {
194 {
195 .addr = client->addr,
196 .flags = client->flags,
197 .len = 2,
198 .buf = (__u8 *)&send_buf,
199 },
200 {
201 .addr = client->addr,
202 .flags = client->flags | I2C_M_RD,
203 .buf = (__u8 *)&rcv_buf,
204 },
205 };
206
207
208 send_buf = cpu_to_be16(MS_SENSORS_SERIAL_READ_MSB);
209 msg[1].len = 8;
210 ret = i2c_transfer(client->adapter, msg, 2);
211 if (ret < 0) {
212 dev_err(&client->dev, "Unable to read device serial number");
213 return ret;
214 }
215
216 rcv_val = be64_to_cpu(rcv_buf);
217 dev_dbg(&client->dev, "Serial MSB raw : %llx\n", rcv_val);
218
219 for (i = 0; i < 64; i += 16) {
220 if (!ms_sensors_crc_valid((rcv_val >> i) & 0xFFFF))
221 return -ENODEV;
222 }
223
224 *sn = (((rcv_val >> 32) & 0xFF000000) |
225 ((rcv_val >> 24) & 0x00FF0000) |
226 ((rcv_val >> 16) & 0x0000FF00) |
227 ((rcv_val >> 8) & 0x000000FF)) << 16;
228
229
230 send_buf = cpu_to_be16(MS_SENSORS_SERIAL_READ_LSB);
231 msg[1].len = 6;
232 rcv_buf = 0;
233 ret = i2c_transfer(client->adapter, msg, 2);
234 if (ret < 0) {
235 dev_err(&client->dev, "Unable to read device serial number");
236 return ret;
237 }
238
239 rcv_val = be64_to_cpu(rcv_buf) >> 16;
240 dev_dbg(&client->dev, "Serial MSB raw : %llx\n", rcv_val);
241
242 for (i = 0; i < 48; i += 24) {
243 if (!ms_sensors_crc_valid((rcv_val >> i) & 0xFFFFFF))
244 return -ENODEV;
245 }
246
247 *sn |= (rcv_val & 0xFFFF00) << 40 | (rcv_val >> 32);
248
249 return 0;
250}
251EXPORT_SYMBOL(ms_sensors_read_serial);
252
253static int ms_sensors_read_config_reg(struct i2c_client *client,
254 u8 *config_reg)
255{
256 int ret;
257
258 ret = i2c_smbus_write_byte(client, MS_SENSORS_CONFIG_REG_READ);
259 if (ret) {
260 dev_err(&client->dev, "Unable to read config register");
261 return ret;
262 }
263
264 ret = i2c_master_recv(client, config_reg, 1);
265 if (ret < 0) {
266 dev_err(&client->dev, "Unable to read config register");
267 return ret;
268 }
269 dev_dbg(&client->dev, "Config register :%x\n", *config_reg);
270
271 return 0;
272}
273
274
275
276
277
278
279
280
281
282
283
284
285ssize_t ms_sensors_write_resolution(struct ms_ht_dev *dev_data,
286 u8 i)
287{
288 u8 config_reg;
289 int ret;
290
291 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
292 if (ret)
293 return ret;
294
295 config_reg &= 0x7E;
296 config_reg |= ((i & 1) << 7) + ((i & 2) >> 1);
297
298 return i2c_smbus_write_byte_data(dev_data->client,
299 MS_SENSORS_CONFIG_REG_WRITE,
300 config_reg);
301}
302EXPORT_SYMBOL(ms_sensors_write_resolution);
303
304
305
306
307
308
309
310
311
312
313
314
315ssize_t ms_sensors_show_battery_low(struct ms_ht_dev *dev_data,
316 char *buf)
317{
318 int ret;
319 u8 config_reg;
320
321 mutex_lock(&dev_data->lock);
322 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
323 mutex_unlock(&dev_data->lock);
324 if (ret)
325 return ret;
326
327 return sprintf(buf, "%d\n", (config_reg & 0x40) >> 6);
328}
329EXPORT_SYMBOL(ms_sensors_show_battery_low);
330
331
332
333
334
335
336
337
338
339
340
341
342ssize_t ms_sensors_show_heater(struct ms_ht_dev *dev_data,
343 char *buf)
344{
345 u8 config_reg;
346 int ret;
347
348 mutex_lock(&dev_data->lock);
349 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
350 mutex_unlock(&dev_data->lock);
351 if (ret)
352 return ret;
353
354 return sprintf(buf, "%d\n", (config_reg & 0x4) >> 2);
355}
356EXPORT_SYMBOL(ms_sensors_show_heater);
357
358
359
360
361
362
363
364
365
366
367
368
369
370ssize_t ms_sensors_write_heater(struct ms_ht_dev *dev_data,
371 const char *buf, size_t len)
372{
373 u8 val, config_reg;
374 int ret;
375
376 ret = kstrtou8(buf, 10, &val);
377 if (ret)
378 return ret;
379
380 if (val > 1)
381 return -EINVAL;
382
383 mutex_lock(&dev_data->lock);
384 ret = ms_sensors_read_config_reg(dev_data->client, &config_reg);
385 if (ret) {
386 mutex_unlock(&dev_data->lock);
387 return ret;
388 }
389
390 config_reg &= 0xFB;
391 config_reg |= val << 2;
392
393 ret = i2c_smbus_write_byte_data(dev_data->client,
394 MS_SENSORS_CONFIG_REG_WRITE,
395 config_reg);
396 mutex_unlock(&dev_data->lock);
397 if (ret) {
398 dev_err(&dev_data->client->dev, "Unable to write config register\n");
399 return ret;
400 }
401
402 return len;
403}
404EXPORT_SYMBOL(ms_sensors_write_heater);
405
406
407
408
409
410
411
412
413
414
415
416
417int ms_sensors_ht_read_temperature(struct ms_ht_dev *dev_data,
418 s32 *temperature)
419{
420 int ret;
421 u32 adc;
422 u16 delay;
423
424 mutex_lock(&dev_data->lock);
425 delay = ms_sensors_ht_t_conversion_time[dev_data->res_index];
426 ret = ms_sensors_convert_and_read(dev_data->client,
427 MS_SENSORS_HT_T_CONVERSION_START,
428 MS_SENSORS_NO_READ_CMD,
429 delay, &adc);
430 mutex_unlock(&dev_data->lock);
431 if (ret)
432 return ret;
433
434 if (!ms_sensors_crc_valid(adc)) {
435 dev_err(&dev_data->client->dev,
436 "Temperature read crc check error\n");
437 return -ENODEV;
438 }
439
440
441 *temperature = (((s64)(adc >> 8) * 175720) >> 16) - 46850;
442
443 return 0;
444}
445EXPORT_SYMBOL(ms_sensors_ht_read_temperature);
446
447
448
449
450
451
452
453
454
455
456
457
458int ms_sensors_ht_read_humidity(struct ms_ht_dev *dev_data,
459 u32 *humidity)
460{
461 int ret;
462 u32 adc;
463 u16 delay;
464
465 mutex_lock(&dev_data->lock);
466 delay = ms_sensors_ht_h_conversion_time[dev_data->res_index];
467 ret = ms_sensors_convert_and_read(dev_data->client,
468 MS_SENSORS_HT_H_CONVERSION_START,
469 MS_SENSORS_NO_READ_CMD,
470 delay, &adc);
471 mutex_unlock(&dev_data->lock);
472 if (ret)
473 return ret;
474
475 if (!ms_sensors_crc_valid(adc)) {
476 dev_err(&dev_data->client->dev,
477 "Humidity read crc check error\n");
478 return -ENODEV;
479 }
480
481
482 *humidity = (((s32)(adc >> 8) * 12500) >> 16) * 10 - 6000;
483 if (*humidity >= 100000)
484 *humidity = 100000;
485
486 return 0;
487}
488EXPORT_SYMBOL(ms_sensors_ht_read_humidity);
489
490
491
492
493
494
495
496
497
498
499
500static bool ms_sensors_tp_crc_valid(u16 *prom, u8 len)
501{
502 unsigned int cnt, n_bit;
503 u16 n_rem = 0x0000, crc_read = prom[0], crc = (*prom & 0xF000) >> 12;
504
505 prom[len - 1] = 0;
506 prom[0] &= 0x0FFF;
507
508 for (cnt = 0; cnt < len * 2; cnt++) {
509 if (cnt % 2 == 1)
510 n_rem ^= prom[cnt >> 1] & 0x00FF;
511 else
512 n_rem ^= prom[cnt >> 1] >> 8;
513
514 for (n_bit = 8; n_bit > 0; n_bit--) {
515 if (n_rem & 0x8000)
516 n_rem = (n_rem << 1) ^ 0x3000;
517 else
518 n_rem <<= 1;
519 }
520 }
521 n_rem >>= 12;
522 prom[0] = crc_read;
523
524 return n_rem == crc;
525}
526
527
528
529
530
531
532
533
534
535
536int ms_sensors_tp_read_prom(struct ms_tp_dev *dev_data)
537{
538 int i, ret;
539
540 for (i = 0; i < MS_SENSORS_TP_PROM_WORDS_NB; i++) {
541 ret = ms_sensors_read_prom_word(
542 dev_data->client,
543 MS_SENSORS_TP_PROM_READ + (i << 1),
544 &dev_data->prom[i]);
545
546 if (ret)
547 return ret;
548 }
549
550 if (!ms_sensors_tp_crc_valid(dev_data->prom,
551 MS_SENSORS_TP_PROM_WORDS_NB + 1)) {
552 dev_err(&dev_data->client->dev,
553 "Calibration coefficients crc check error\n");
554 return -ENODEV;
555 }
556
557 return 0;
558}
559EXPORT_SYMBOL(ms_sensors_tp_read_prom);
560
561
562
563
564
565
566
567
568
569
570
571
572int ms_sensors_read_temp_and_pressure(struct ms_tp_dev *dev_data,
573 int *temperature,
574 unsigned int *pressure)
575{
576 int ret;
577 u32 t_adc, p_adc;
578 s32 dt, temp;
579 s64 off, sens, t2, off2, sens2;
580 u16 *prom = dev_data->prom, delay;
581
582 mutex_lock(&dev_data->lock);
583 delay = ms_sensors_tp_conversion_time[dev_data->res_index];
584
585 ret = ms_sensors_convert_and_read(
586 dev_data->client,
587 MS_SENSORS_TP_T_CONVERSION_START +
588 dev_data->res_index * 2,
589 MS_SENSORS_TP_ADC_READ,
590 delay, &t_adc);
591 if (ret) {
592 mutex_unlock(&dev_data->lock);
593 return ret;
594 }
595
596 ret = ms_sensors_convert_and_read(
597 dev_data->client,
598 MS_SENSORS_TP_P_CONVERSION_START +
599 dev_data->res_index * 2,
600 MS_SENSORS_TP_ADC_READ,
601 delay, &p_adc);
602 mutex_unlock(&dev_data->lock);
603 if (ret)
604 return ret;
605
606 dt = (s32)t_adc - (prom[5] << 8);
607
608
609 temp = 2000 + (((s64)dt * prom[6]) >> 23);
610
611
612 if (temp < 2000) {
613 s64 tmp = (s64)temp - 2000;
614
615 t2 = (3 * ((s64)dt * (s64)dt)) >> 33;
616 off2 = (61 * tmp * tmp) >> 4;
617 sens2 = (29 * tmp * tmp) >> 4;
618
619 if (temp < -1500) {
620 s64 tmp = (s64)temp + 1500;
621
622 off2 += 17 * tmp * tmp;
623 sens2 += 9 * tmp * tmp;
624 }
625 } else {
626 t2 = (5 * ((s64)dt * (s64)dt)) >> 38;
627 off2 = 0;
628 sens2 = 0;
629 }
630
631
632 off = (((s64)prom[2]) << 17) + ((((s64)prom[4]) * (s64)dt) >> 6);
633 off -= off2;
634
635
636 sens = (((s64)prom[1]) << 16) + (((s64)prom[3] * dt) >> 7);
637 sens -= sens2;
638
639
640 *temperature = (temp - t2) * 10;
641 *pressure = (u32)(((((s64)p_adc * sens) >> 21) - off) >> 15);
642
643 return 0;
644}
645EXPORT_SYMBOL(ms_sensors_read_temp_and_pressure);
646
647MODULE_DESCRIPTION("Measurement-Specialties common i2c driver");
648MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
649MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
650MODULE_LICENSE("GPL v2");
651
652