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