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