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#include <linux/device.h>
44#include <linux/module.h>
45#include <linux/init.h>
46#include <linux/jiffies.h>
47#include <linux/i2c.h>
48#include <linux/hwmon.h>
49#include <linux/hwmon-sysfs.h>
50#include <linux/err.h>
51#include <linux/mutex.h>
52#include <linux/kernel.h>
53#include <linux/clk.h>
54#include <linux/of.h>
55#include <linux/of_device.h>
56#include <linux/platform_data/g762.h>
57
58#define DRVNAME "g762"
59
60static const struct i2c_device_id g762_id[] = {
61 { "g762", 0 },
62 { "g763", 0 },
63 { }
64};
65MODULE_DEVICE_TABLE(i2c, g762_id);
66
67enum g762_regs {
68 G762_REG_SET_CNT = 0x00,
69 G762_REG_ACT_CNT = 0x01,
70 G762_REG_FAN_STA = 0x02,
71 G762_REG_SET_OUT = 0x03,
72 G762_REG_FAN_CMD1 = 0x04,
73 G762_REG_FAN_CMD2 = 0x05,
74};
75
76
77#define G762_REG_FAN_CMD1_DET_FAN_FAIL 0x80
78#define G762_REG_FAN_CMD1_DET_FAN_OOC 0x40
79#define G762_REG_FAN_CMD1_OUT_MODE 0x20
80#define G762_REG_FAN_CMD1_FAN_MODE 0x10
81#define G762_REG_FAN_CMD1_CLK_DIV_ID1 0x08
82#define G762_REG_FAN_CMD1_CLK_DIV_ID0 0x04
83#define G762_REG_FAN_CMD1_PWM_POLARITY 0x02
84#define G762_REG_FAN_CMD1_PULSE_PER_REV 0x01
85
86#define G762_REG_FAN_CMD2_GEAR_MODE_1 0x08
87#define G762_REG_FAN_CMD2_GEAR_MODE_0 0x04
88#define G762_REG_FAN_CMD2_FAN_STARTV_1 0x02
89#define G762_REG_FAN_CMD2_FAN_STARTV_0 0x01
90
91#define G762_REG_FAN_STA_FAIL 0x02
92#define G762_REG_FAN_STA_OOC 0x01
93
94
95#define G762_OUT_MODE_PWM 1
96#define G762_OUT_MODE_DC 0
97
98#define G762_FAN_MODE_CLOSED_LOOP 2
99#define G762_FAN_MODE_OPEN_LOOP 1
100
101#define G762_PWM_POLARITY_NEGATIVE 1
102#define G762_PWM_POLARITY_POSITIVE 0
103
104
105#define G762_UPDATE_INTERVAL HZ
106
107
108
109
110
111#define G762_PULSE_FROM_REG(reg) \
112 ((((reg) & G762_REG_FAN_CMD1_PULSE_PER_REV) + 1) << 1)
113
114
115
116
117
118#define G762_CLKDIV_FROM_REG(reg) \
119 (1 << (((reg) & (G762_REG_FAN_CMD1_CLK_DIV_ID0 | \
120 G762_REG_FAN_CMD1_CLK_DIV_ID1)) >> 2))
121
122
123
124
125
126#define G762_GEARMULT_FROM_REG(reg) \
127 (1 << (((reg) & (G762_REG_FAN_CMD2_GEAR_MODE_0 | \
128 G762_REG_FAN_CMD2_GEAR_MODE_1)) >> 2))
129
130struct g762_data {
131 struct device *hwmon_dev;
132 struct i2c_client *client;
133 struct clk *clk;
134
135
136 struct mutex update_lock;
137
138
139 u32 clk_freq;
140
141
142 bool valid;
143 unsigned long last_updated;
144
145 u8 set_cnt;
146 u8 act_cnt;
147 u8 fan_sta;
148
149
150
151
152 u8 set_out;
153 u8 fan_cmd1;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 u8 fan_cmd2;
169
170
171
172
173
174
175};
176
177
178
179
180
181
182
183static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
184 u8 clk_div, u8 gear_mult)
185{
186 if (cnt == 0xff)
187 return 0;
188
189 return (clk_freq * 30 * gear_mult) / ((cnt ? cnt : 1) * p * clk_div);
190}
191
192
193
194
195
196static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
197 u8 clk_div, u8 gear_mult)
198{
199 if (!rpm)
200 return 0xff;
201
202 return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
203 0, 255);
204}
205
206
207static struct g762_data *g762_update_client(struct device *dev)
208{
209 struct g762_data *data = dev_get_drvdata(dev);
210 struct i2c_client *client = data->client;
211 int ret = 0;
212
213 mutex_lock(&data->update_lock);
214 if (time_before(jiffies, data->last_updated + G762_UPDATE_INTERVAL) &&
215 likely(data->valid))
216 goto out;
217
218 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_CNT);
219 if (ret < 0)
220 goto out;
221 data->set_cnt = ret;
222
223 ret = i2c_smbus_read_byte_data(client, G762_REG_ACT_CNT);
224 if (ret < 0)
225 goto out;
226 data->act_cnt = ret;
227
228 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_STA);
229 if (ret < 0)
230 goto out;
231 data->fan_sta = ret;
232
233 ret = i2c_smbus_read_byte_data(client, G762_REG_SET_OUT);
234 if (ret < 0)
235 goto out;
236 data->set_out = ret;
237
238 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD1);
239 if (ret < 0)
240 goto out;
241 data->fan_cmd1 = ret;
242
243 ret = i2c_smbus_read_byte_data(client, G762_REG_FAN_CMD2);
244 if (ret < 0)
245 goto out;
246 data->fan_cmd2 = ret;
247
248 data->last_updated = jiffies;
249 data->valid = true;
250 out:
251 mutex_unlock(&data->update_lock);
252
253 if (ret < 0)
254 data = ERR_PTR(ret);
255
256 return data;
257}
258
259
260
261
262
263
264
265
266
267static int do_set_clk_freq(struct device *dev, unsigned long val)
268{
269 struct g762_data *data = dev_get_drvdata(dev);
270
271 if (val > 0xffffff)
272 return -EINVAL;
273 if (!val)
274 val = 32768;
275
276 data->clk_freq = val;
277
278 return 0;
279}
280
281
282static int do_set_pwm_mode(struct device *dev, unsigned long val)
283{
284 struct g762_data *data = g762_update_client(dev);
285 int ret;
286
287 if (IS_ERR(data))
288 return PTR_ERR(data);
289
290 mutex_lock(&data->update_lock);
291 switch (val) {
292 case G762_OUT_MODE_PWM:
293 data->fan_cmd1 |= G762_REG_FAN_CMD1_OUT_MODE;
294 break;
295 case G762_OUT_MODE_DC:
296 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_OUT_MODE;
297 break;
298 default:
299 ret = -EINVAL;
300 goto out;
301 }
302 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
303 data->fan_cmd1);
304 data->valid = false;
305 out:
306 mutex_unlock(&data->update_lock);
307
308 return ret;
309}
310
311
312static int do_set_fan_div(struct device *dev, unsigned long val)
313{
314 struct g762_data *data = g762_update_client(dev);
315 int ret;
316
317 if (IS_ERR(data))
318 return PTR_ERR(data);
319
320 mutex_lock(&data->update_lock);
321 switch (val) {
322 case 1:
323 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
324 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
325 break;
326 case 2:
327 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
328 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID1;
329 break;
330 case 4:
331 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_CLK_DIV_ID0;
332 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
333 break;
334 case 8:
335 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID0;
336 data->fan_cmd1 |= G762_REG_FAN_CMD1_CLK_DIV_ID1;
337 break;
338 default:
339 ret = -EINVAL;
340 goto out;
341 }
342 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
343 data->fan_cmd1);
344 data->valid = false;
345 out:
346 mutex_unlock(&data->update_lock);
347
348 return ret;
349}
350
351
352static int do_set_fan_gear_mode(struct device *dev, unsigned long val)
353{
354 struct g762_data *data = g762_update_client(dev);
355 int ret;
356
357 if (IS_ERR(data))
358 return PTR_ERR(data);
359
360 mutex_lock(&data->update_lock);
361 switch (val) {
362 case 0:
363 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
364 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
365 break;
366 case 1:
367 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_0;
368 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_1;
369 break;
370 case 2:
371 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_GEAR_MODE_0;
372 data->fan_cmd2 |= G762_REG_FAN_CMD2_GEAR_MODE_1;
373 break;
374 default:
375 ret = -EINVAL;
376 goto out;
377 }
378 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
379 data->fan_cmd2);
380 data->valid = false;
381 out:
382 mutex_unlock(&data->update_lock);
383
384 return ret;
385}
386
387
388static int do_set_fan_pulses(struct device *dev, unsigned long val)
389{
390 struct g762_data *data = g762_update_client(dev);
391 int ret;
392
393 if (IS_ERR(data))
394 return PTR_ERR(data);
395
396 mutex_lock(&data->update_lock);
397 switch (val) {
398 case 2:
399 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PULSE_PER_REV;
400 break;
401 case 4:
402 data->fan_cmd1 |= G762_REG_FAN_CMD1_PULSE_PER_REV;
403 break;
404 default:
405 ret = -EINVAL;
406 goto out;
407 }
408 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
409 data->fan_cmd1);
410 data->valid = false;
411 out:
412 mutex_unlock(&data->update_lock);
413
414 return ret;
415}
416
417
418static int do_set_pwm_enable(struct device *dev, unsigned long val)
419{
420 struct g762_data *data = g762_update_client(dev);
421 int ret;
422
423 if (IS_ERR(data))
424 return PTR_ERR(data);
425
426 mutex_lock(&data->update_lock);
427 switch (val) {
428 case G762_FAN_MODE_CLOSED_LOOP:
429 data->fan_cmd1 |= G762_REG_FAN_CMD1_FAN_MODE;
430 break;
431 case G762_FAN_MODE_OPEN_LOOP:
432 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_FAN_MODE;
433
434
435
436
437
438
439
440 if (data->set_cnt == 0xff)
441 i2c_smbus_write_byte_data(data->client,
442 G762_REG_SET_CNT, 254);
443 break;
444 default:
445 ret = -EINVAL;
446 goto out;
447 }
448
449 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
450 data->fan_cmd1);
451 data->valid = false;
452 out:
453 mutex_unlock(&data->update_lock);
454
455 return ret;
456}
457
458
459static int do_set_pwm_polarity(struct device *dev, unsigned long val)
460{
461 struct g762_data *data = g762_update_client(dev);
462 int ret;
463
464 if (IS_ERR(data))
465 return PTR_ERR(data);
466
467 mutex_lock(&data->update_lock);
468 switch (val) {
469 case G762_PWM_POLARITY_POSITIVE:
470 data->fan_cmd1 &= ~G762_REG_FAN_CMD1_PWM_POLARITY;
471 break;
472 case G762_PWM_POLARITY_NEGATIVE:
473 data->fan_cmd1 |= G762_REG_FAN_CMD1_PWM_POLARITY;
474 break;
475 default:
476 ret = -EINVAL;
477 goto out;
478 }
479 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
480 data->fan_cmd1);
481 data->valid = false;
482 out:
483 mutex_unlock(&data->update_lock);
484
485 return ret;
486}
487
488
489
490
491
492static int do_set_pwm(struct device *dev, unsigned long val)
493{
494 struct g762_data *data = dev_get_drvdata(dev);
495 struct i2c_client *client = data->client;
496 int ret;
497
498 if (val > 255)
499 return -EINVAL;
500
501 mutex_lock(&data->update_lock);
502 ret = i2c_smbus_write_byte_data(client, G762_REG_SET_OUT, val);
503 data->valid = false;
504 mutex_unlock(&data->update_lock);
505
506 return ret;
507}
508
509
510
511
512
513static int do_set_fan_target(struct device *dev, unsigned long val)
514{
515 struct g762_data *data = g762_update_client(dev);
516 int ret;
517
518 if (IS_ERR(data))
519 return PTR_ERR(data);
520
521 mutex_lock(&data->update_lock);
522 data->set_cnt = cnt_from_rpm(val, data->clk_freq,
523 G762_PULSE_FROM_REG(data->fan_cmd1),
524 G762_CLKDIV_FROM_REG(data->fan_cmd1),
525 G762_GEARMULT_FROM_REG(data->fan_cmd2));
526 ret = i2c_smbus_write_byte_data(data->client, G762_REG_SET_CNT,
527 data->set_cnt);
528 data->valid = false;
529 mutex_unlock(&data->update_lock);
530
531 return ret;
532}
533
534
535static int do_set_fan_startv(struct device *dev, unsigned long val)
536{
537 struct g762_data *data = g762_update_client(dev);
538 int ret;
539
540 if (IS_ERR(data))
541 return PTR_ERR(data);
542
543 mutex_lock(&data->update_lock);
544 switch (val) {
545 case 0:
546 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
547 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
548 break;
549 case 1:
550 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
551 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_1;
552 break;
553 case 2:
554 data->fan_cmd2 &= ~G762_REG_FAN_CMD2_FAN_STARTV_0;
555 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
556 break;
557 case 3:
558 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_0;
559 data->fan_cmd2 |= G762_REG_FAN_CMD2_FAN_STARTV_1;
560 break;
561 default:
562 ret = -EINVAL;
563 goto out;
564 }
565 ret = i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD2,
566 data->fan_cmd2);
567 data->valid = false;
568 out:
569 mutex_unlock(&data->update_lock);
570
571 return ret;
572}
573
574
575
576
577
578
579#ifdef CONFIG_OF
580static const struct of_device_id g762_dt_match[] = {
581 { .compatible = "gmt,g762" },
582 { .compatible = "gmt,g763" },
583 { },
584};
585MODULE_DEVICE_TABLE(of, g762_dt_match);
586
587
588
589
590
591
592
593
594static int g762_of_clock_enable(struct i2c_client *client)
595{
596 struct g762_data *data;
597 unsigned long clk_freq;
598 struct clk *clk;
599 int ret;
600
601 if (!client->dev.of_node)
602 return 0;
603
604 clk = of_clk_get(client->dev.of_node, 0);
605 if (IS_ERR(clk)) {
606 dev_err(&client->dev, "failed to get clock\n");
607 return PTR_ERR(clk);
608 }
609
610 ret = clk_prepare_enable(clk);
611 if (ret) {
612 dev_err(&client->dev, "failed to enable clock\n");
613 goto clk_put;
614 }
615
616 clk_freq = clk_get_rate(clk);
617 ret = do_set_clk_freq(&client->dev, clk_freq);
618 if (ret) {
619 dev_err(&client->dev, "invalid clock freq %lu\n", clk_freq);
620 goto clk_unprep;
621 }
622
623 data = i2c_get_clientdata(client);
624 data->clk = clk;
625
626 return 0;
627
628 clk_unprep:
629 clk_disable_unprepare(clk);
630
631 clk_put:
632 clk_put(clk);
633
634 return ret;
635}
636
637static void g762_of_clock_disable(struct i2c_client *client)
638{
639 struct g762_data *data = i2c_get_clientdata(client);
640
641 if (!data->clk)
642 return;
643
644 clk_disable_unprepare(data->clk);
645 clk_put(data->clk);
646}
647
648static int g762_of_prop_import_one(struct i2c_client *client,
649 const char *pname,
650 int (*psetter)(struct device *dev,
651 unsigned long val))
652{
653 int ret;
654 u32 pval;
655
656 if (of_property_read_u32(client->dev.of_node, pname, &pval))
657 return 0;
658
659 dev_dbg(&client->dev, "found %s (%d)\n", pname, pval);
660 ret = (*psetter)(&client->dev, pval);
661 if (ret)
662 dev_err(&client->dev, "unable to set %s (%d)\n", pname, pval);
663
664 return ret;
665}
666
667static int g762_of_prop_import(struct i2c_client *client)
668{
669 int ret;
670
671 if (!client->dev.of_node)
672 return 0;
673
674 ret = g762_of_prop_import_one(client, "fan_gear_mode",
675 do_set_fan_gear_mode);
676 if (ret)
677 return ret;
678
679 ret = g762_of_prop_import_one(client, "pwm_polarity",
680 do_set_pwm_polarity);
681 if (ret)
682 return ret;
683
684 return g762_of_prop_import_one(client, "fan_startv",
685 do_set_fan_startv);
686}
687
688#else
689static int g762_of_prop_import(struct i2c_client *client)
690{
691 return 0;
692}
693
694static int g762_of_clock_enable(struct i2c_client *client)
695{
696 return 0;
697}
698
699static void g762_of_clock_disable(struct i2c_client *client) { }
700#endif
701
702
703
704
705
706
707static int g762_pdata_prop_import(struct i2c_client *client)
708{
709 struct g762_platform_data *pdata = dev_get_platdata(&client->dev);
710 int ret;
711
712 if (!pdata)
713 return 0;
714
715 ret = do_set_fan_gear_mode(&client->dev, pdata->fan_gear_mode);
716 if (ret)
717 return ret;
718
719 ret = do_set_pwm_polarity(&client->dev, pdata->pwm_polarity);
720 if (ret)
721 return ret;
722
723 ret = do_set_fan_startv(&client->dev, pdata->fan_startv);
724 if (ret)
725 return ret;
726
727 return do_set_clk_freq(&client->dev, pdata->clk_freq);
728}
729
730
731
732
733
734
735
736
737
738static ssize_t get_fan_rpm(struct device *dev, struct device_attribute *da,
739 char *buf)
740{
741 struct g762_data *data = g762_update_client(dev);
742 unsigned int rpm = 0;
743
744 if (IS_ERR(data))
745 return PTR_ERR(data);
746
747 mutex_lock(&data->update_lock);
748
749 if (data->fan_sta & G762_REG_FAN_STA_OOC) {
750 rpm = rpm_from_cnt(data->act_cnt, data->clk_freq,
751 G762_PULSE_FROM_REG(data->fan_cmd1),
752 G762_CLKDIV_FROM_REG(data->fan_cmd1),
753 G762_GEARMULT_FROM_REG(data->fan_cmd2));
754 }
755 mutex_unlock(&data->update_lock);
756
757 return sprintf(buf, "%u\n", rpm);
758}
759
760
761
762
763
764static ssize_t get_pwm_mode(struct device *dev, struct device_attribute *da,
765 char *buf)
766{
767 struct g762_data *data = g762_update_client(dev);
768
769 if (IS_ERR(data))
770 return PTR_ERR(data);
771
772 return sprintf(buf, "%d\n",
773 !!(data->fan_cmd1 & G762_REG_FAN_CMD1_OUT_MODE));
774}
775
776static ssize_t set_pwm_mode(struct device *dev, struct device_attribute *da,
777 const char *buf, size_t count)
778{
779 unsigned long val;
780 int ret;
781
782 if (kstrtoul(buf, 10, &val))
783 return -EINVAL;
784
785 ret = do_set_pwm_mode(dev, val);
786 if (ret < 0)
787 return ret;
788
789 return count;
790}
791
792
793
794
795
796static ssize_t get_fan_div(struct device *dev,
797 struct device_attribute *da, char *buf)
798{
799 struct g762_data *data = g762_update_client(dev);
800
801 if (IS_ERR(data))
802 return PTR_ERR(data);
803
804 return sprintf(buf, "%d\n", G762_CLKDIV_FROM_REG(data->fan_cmd1));
805}
806
807static ssize_t set_fan_div(struct device *dev,
808 struct device_attribute *da,
809 const char *buf, size_t count)
810{
811 unsigned long val;
812 int ret;
813
814 if (kstrtoul(buf, 10, &val))
815 return -EINVAL;
816
817 ret = do_set_fan_div(dev, val);
818 if (ret < 0)
819 return ret;
820
821 return count;
822}
823
824
825
826
827
828static ssize_t get_fan_pulses(struct device *dev,
829 struct device_attribute *da, char *buf)
830{
831 struct g762_data *data = g762_update_client(dev);
832
833 if (IS_ERR(data))
834 return PTR_ERR(data);
835
836 return sprintf(buf, "%d\n", G762_PULSE_FROM_REG(data->fan_cmd1));
837}
838
839static ssize_t set_fan_pulses(struct device *dev,
840 struct device_attribute *da,
841 const char *buf, size_t count)
842{
843 unsigned long val;
844 int ret;
845
846 if (kstrtoul(buf, 10, &val))
847 return -EINVAL;
848
849 ret = do_set_fan_pulses(dev, val);
850 if (ret < 0)
851 return ret;
852
853 return count;
854}
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870static ssize_t get_pwm_enable(struct device *dev,
871 struct device_attribute *da, char *buf)
872{
873 struct g762_data *data = g762_update_client(dev);
874
875 if (IS_ERR(data))
876 return PTR_ERR(data);
877
878 return sprintf(buf, "%d\n",
879 (!!(data->fan_cmd1 & G762_REG_FAN_CMD1_FAN_MODE)) + 1);
880}
881
882static ssize_t set_pwm_enable(struct device *dev,
883 struct device_attribute *da,
884 const char *buf, size_t count)
885{
886 unsigned long val;
887 int ret;
888
889 if (kstrtoul(buf, 10, &val))
890 return -EINVAL;
891
892 ret = do_set_pwm_enable(dev, val);
893 if (ret < 0)
894 return ret;
895
896 return count;
897}
898
899
900
901
902
903
904static ssize_t get_pwm(struct device *dev, struct device_attribute *da,
905 char *buf)
906{
907 struct g762_data *data = g762_update_client(dev);
908
909 if (IS_ERR(data))
910 return PTR_ERR(data);
911
912 return sprintf(buf, "%d\n", data->set_out);
913}
914
915static ssize_t set_pwm(struct device *dev, struct device_attribute *da,
916 const char *buf, size_t count)
917{
918 unsigned long val;
919 int ret;
920
921 if (kstrtoul(buf, 10, &val))
922 return -EINVAL;
923
924 ret = do_set_pwm(dev, val);
925 if (ret < 0)
926 return ret;
927
928 return count;
929}
930
931
932
933
934
935
936
937
938
939
940
941
942static ssize_t get_fan_target(struct device *dev, struct device_attribute *da,
943 char *buf)
944{
945 struct g762_data *data = g762_update_client(dev);
946 unsigned int rpm;
947
948 if (IS_ERR(data))
949 return PTR_ERR(data);
950
951 mutex_lock(&data->update_lock);
952 rpm = rpm_from_cnt(data->set_cnt, data->clk_freq,
953 G762_PULSE_FROM_REG(data->fan_cmd1),
954 G762_CLKDIV_FROM_REG(data->fan_cmd1),
955 G762_GEARMULT_FROM_REG(data->fan_cmd2));
956 mutex_unlock(&data->update_lock);
957
958 return sprintf(buf, "%u\n", rpm);
959}
960
961static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
962 const char *buf, size_t count)
963{
964 unsigned long val;
965 int ret;
966
967 if (kstrtoul(buf, 10, &val))
968 return -EINVAL;
969
970 ret = do_set_fan_target(dev, val);
971 if (ret < 0)
972 return ret;
973
974 return count;
975}
976
977
978static ssize_t get_fan_failure(struct device *dev, struct device_attribute *da,
979 char *buf)
980{
981 struct g762_data *data = g762_update_client(dev);
982
983 if (IS_ERR(data))
984 return PTR_ERR(data);
985
986 return sprintf(buf, "%u\n", !!(data->fan_sta & G762_REG_FAN_STA_FAIL));
987}
988
989
990
991
992
993static ssize_t get_fan_ooc(struct device *dev, struct device_attribute *da,
994 char *buf)
995{
996 struct g762_data *data = g762_update_client(dev);
997
998 if (IS_ERR(data))
999 return PTR_ERR(data);
1000
1001 return sprintf(buf, "%u\n", !(data->fan_sta & G762_REG_FAN_STA_OOC));
1002}
1003
1004static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
1005static DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, get_pwm_mode, set_pwm_mode);
1006static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
1007 get_pwm_enable, set_pwm_enable);
1008static DEVICE_ATTR(fan1_input, S_IRUGO, get_fan_rpm, NULL);
1009static DEVICE_ATTR(fan1_alarm, S_IRUGO, get_fan_ooc, NULL);
1010static DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_failure, NULL);
1011static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
1012 get_fan_target, set_fan_target);
1013static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_fan_div, set_fan_div);
1014static DEVICE_ATTR(fan1_pulses, S_IWUSR | S_IRUGO,
1015 get_fan_pulses, set_fan_pulses);
1016
1017
1018static struct attribute *g762_attrs[] = {
1019 &dev_attr_fan1_input.attr,
1020 &dev_attr_fan1_alarm.attr,
1021 &dev_attr_fan1_fault.attr,
1022 &dev_attr_fan1_target.attr,
1023 &dev_attr_fan1_div.attr,
1024 &dev_attr_fan1_pulses.attr,
1025 &dev_attr_pwm1.attr,
1026 &dev_attr_pwm1_mode.attr,
1027 &dev_attr_pwm1_enable.attr,
1028 NULL
1029};
1030
1031ATTRIBUTE_GROUPS(g762);
1032
1033
1034
1035
1036
1037
1038static inline int g762_fan_init(struct device *dev)
1039{
1040 struct g762_data *data = g762_update_client(dev);
1041
1042 if (IS_ERR(data))
1043 return PTR_ERR(data);
1044
1045 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_FAIL;
1046 data->fan_cmd1 |= G762_REG_FAN_CMD1_DET_FAN_OOC;
1047 data->valid = false;
1048
1049 return i2c_smbus_write_byte_data(data->client, G762_REG_FAN_CMD1,
1050 data->fan_cmd1);
1051}
1052
1053static int g762_probe(struct i2c_client *client, const struct i2c_device_id *id)
1054{
1055 struct device *dev = &client->dev;
1056 struct g762_data *data;
1057 int ret;
1058
1059 if (!i2c_check_functionality(client->adapter,
1060 I2C_FUNC_SMBUS_BYTE_DATA))
1061 return -ENODEV;
1062
1063 data = devm_kzalloc(dev, sizeof(struct g762_data), GFP_KERNEL);
1064 if (!data)
1065 return -ENOMEM;
1066
1067 i2c_set_clientdata(client, data);
1068 data->client = client;
1069 mutex_init(&data->update_lock);
1070
1071
1072 ret = g762_fan_init(dev);
1073 if (ret)
1074 return ret;
1075
1076
1077 ret = g762_of_clock_enable(client);
1078 if (ret)
1079 return ret;
1080 ret = g762_of_prop_import(client);
1081 if (ret)
1082 goto clock_dis;
1083
1084 ret = g762_pdata_prop_import(client);
1085 if (ret)
1086 goto clock_dis;
1087
1088 data->hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
1089 data, g762_groups);
1090 if (IS_ERR(data->hwmon_dev)) {
1091 ret = PTR_ERR(data->hwmon_dev);
1092 goto clock_dis;
1093 }
1094
1095 return 0;
1096
1097 clock_dis:
1098 g762_of_clock_disable(client);
1099
1100 return ret;
1101}
1102
1103static int g762_remove(struct i2c_client *client)
1104{
1105 struct g762_data *data = i2c_get_clientdata(client);
1106
1107 hwmon_device_unregister(data->hwmon_dev);
1108 g762_of_clock_disable(client);
1109
1110 return 0;
1111}
1112
1113static struct i2c_driver g762_driver = {
1114 .driver = {
1115 .name = DRVNAME,
1116 .owner = THIS_MODULE,
1117 .of_match_table = of_match_ptr(g762_dt_match),
1118 },
1119 .probe = g762_probe,
1120 .remove = g762_remove,
1121 .id_table = g762_id,
1122};
1123
1124module_i2c_driver(g762_driver);
1125
1126MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
1127MODULE_DESCRIPTION("GMT G762/G763 driver");
1128MODULE_LICENSE("GPL");
1129