xref: /OK3568_Linux_fs/kernel/drivers/soc/rockchip/rockchip_system_monitor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  * Author: Finley Xiao <finley.xiao@rock-chips.com>
5  */
6 
7 #include <dt-bindings/soc/rockchip-system-status.h>
8 #include <linux/clk-provider.h>
9 #include <linux/cpu.h>
10 #include <linux/cpufreq.h>
11 #include <linux/devfreq.h>
12 #include <linux/device.h>
13 #include <linux/fb.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_opp.h>
20 #include <linux/pm_qos.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/coupler.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/reboot.h>
27 #include <linux/slab.h>
28 #include <linux/suspend.h>
29 #include <linux/thermal.h>
30 #include <linux/uaccess.h>
31 #include <linux/version.h>
32 #include <linux/delay.h>
33 #include <soc/rockchip/rockchip_opp_select.h>
34 #include <soc/rockchip/rockchip_system_monitor.h>
35 #include <soc/rockchip/rockchip-system-status.h>
36 
37 #include "../../gpu/drm/rockchip/ebc-dev/ebc_dev.h"
38 #include "../../opp/opp.h"
39 #include "../../regulator/internal.h"
40 #include "../../thermal/thermal_core.h"
41 
42 #define CPU_REBOOT_FREQ		816000 /* kHz */
43 #define VIDEO_1080P_SIZE	(1920 * 1080)
44 #define THERMAL_POLLING_DELAY	200 /* milliseconds */
45 
46 struct video_info {
47 	unsigned int width;
48 	unsigned int height;
49 	unsigned int ishevc;
50 	unsigned int videoFramerate;
51 	unsigned int streamBitrate;
52 	struct list_head node;
53 };
54 
55 struct system_monitor_attr {
56 	struct attribute attr;
57 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
58 			char *buf);
59 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
60 			 const char *buf, size_t n);
61 };
62 
63 struct system_monitor {
64 	struct device *dev;
65 	struct cpumask video_4k_offline_cpus;
66 	struct cpumask status_offline_cpus;
67 	struct cpumask temp_offline_cpus;
68 	struct cpumask offline_cpus;
69 	struct notifier_block status_nb;
70 	struct kobject *kobj;
71 
72 	struct thermal_zone_device *tz;
73 	struct delayed_work thermal_work;
74 	int last_temp;
75 	int offline_cpus_temp;
76 	int temp_hysteresis;
77 	unsigned int delay;
78 	bool is_temp_offline;
79 };
80 
81 static unsigned long system_status;
82 static unsigned long ref_count[32] = {0};
83 
84 static DEFINE_MUTEX(system_status_mutex);
85 static DEFINE_MUTEX(video_info_mutex);
86 static DEFINE_MUTEX(cpu_on_off_mutex);
87 
88 static DECLARE_RWSEM(mdev_list_sem);
89 
90 static LIST_HEAD(video_info_list);
91 static LIST_HEAD(monitor_dev_list);
92 static struct system_monitor *system_monitor;
93 static atomic_t monitor_in_suspend;
94 
95 static BLOCKING_NOTIFIER_HEAD(system_monitor_notifier_list);
96 static BLOCKING_NOTIFIER_HEAD(system_status_notifier_list);
97 
rockchip_register_system_status_notifier(struct notifier_block * nb)98 int rockchip_register_system_status_notifier(struct notifier_block *nb)
99 {
100 	return blocking_notifier_chain_register(&system_status_notifier_list,
101 						nb);
102 }
103 EXPORT_SYMBOL(rockchip_register_system_status_notifier);
104 
rockchip_unregister_system_status_notifier(struct notifier_block * nb)105 int rockchip_unregister_system_status_notifier(struct notifier_block *nb)
106 {
107 	return blocking_notifier_chain_unregister(&system_status_notifier_list,
108 						  nb);
109 }
110 EXPORT_SYMBOL(rockchip_unregister_system_status_notifier);
111 
rockchip_system_status_notifier_call_chain(unsigned long val)112 static int rockchip_system_status_notifier_call_chain(unsigned long val)
113 {
114 	int ret = blocking_notifier_call_chain(&system_status_notifier_list,
115 					       val, NULL);
116 
117 	return notifier_to_errno(ret);
118 }
119 
rockchip_set_system_status(unsigned long status)120 void rockchip_set_system_status(unsigned long status)
121 {
122 	unsigned long old_system_status;
123 	unsigned int single_status_offset;
124 
125 	mutex_lock(&system_status_mutex);
126 
127 	old_system_status = system_status;
128 
129 	while (status) {
130 		single_status_offset = fls(status) - 1;
131 		status &= ~(1 << single_status_offset);
132 		if (ref_count[single_status_offset] == 0)
133 			system_status |= 1 << single_status_offset;
134 		ref_count[single_status_offset]++;
135 	}
136 
137 	if (old_system_status != system_status)
138 		rockchip_system_status_notifier_call_chain(system_status);
139 
140 	mutex_unlock(&system_status_mutex);
141 }
142 EXPORT_SYMBOL(rockchip_set_system_status);
143 
rockchip_clear_system_status(unsigned long status)144 void rockchip_clear_system_status(unsigned long status)
145 {
146 	unsigned long old_system_status;
147 	unsigned int single_status_offset;
148 
149 	mutex_lock(&system_status_mutex);
150 
151 	old_system_status = system_status;
152 
153 	while (status) {
154 		single_status_offset = fls(status) - 1;
155 		status &= ~(1 << single_status_offset);
156 		if (ref_count[single_status_offset] == 0) {
157 			continue;
158 		} else {
159 			if (ref_count[single_status_offset] == 1)
160 				system_status &= ~(1 << single_status_offset);
161 			ref_count[single_status_offset]--;
162 		}
163 	}
164 
165 	if (old_system_status != system_status)
166 		rockchip_system_status_notifier_call_chain(system_status);
167 
168 	mutex_unlock(&system_status_mutex);
169 }
170 EXPORT_SYMBOL(rockchip_clear_system_status);
171 
rockchip_get_system_status(void)172 unsigned long rockchip_get_system_status(void)
173 {
174 	return system_status;
175 }
176 EXPORT_SYMBOL(rockchip_get_system_status);
177 
rockchip_add_system_status_interface(struct device * dev)178 int rockchip_add_system_status_interface(struct device *dev)
179 {
180 	if (!system_monitor || !system_monitor->kobj) {
181 		pr_err("failed to get system status kobj\n");
182 		return -EINVAL;
183 	}
184 
185 	return compat_only_sysfs_link_entry_to_kobj(&dev->kobj,
186 						    system_monitor->kobj,
187 						    "system_status", NULL);
188 }
189 EXPORT_SYMBOL(rockchip_add_system_status_interface);
190 
rockchip_get_video_param(char ** str)191 static unsigned long rockchip_get_video_param(char **str)
192 {
193 	char *p;
194 	unsigned long val = 0;
195 
196 	strsep(str, "=");
197 	p = strsep(str, ",");
198 	if (p) {
199 		if (kstrtoul(p, 10, &val))
200 			return 0;
201 	}
202 
203 	return val;
204 }
205 
206 /*
207  * format:
208  * 0,width=val,height=val,ishevc=val,videoFramerate=val,streamBitrate=val
209  * 1,width=val,height=val,ishevc=val,videoFramerate=val,streamBitrate=val
210  */
rockchip_parse_video_info(const char * buf)211 static struct video_info *rockchip_parse_video_info(const char *buf)
212 {
213 	struct video_info *video_info;
214 	const char *cp = buf;
215 	char *str, *p;
216 	int ntokens = 0;
217 
218 	while ((cp = strpbrk(cp + 1, ",")))
219 		ntokens++;
220 	if (ntokens != 5)
221 		return NULL;
222 
223 	video_info = kzalloc(sizeof(*video_info), GFP_KERNEL);
224 	if (!video_info)
225 		return NULL;
226 
227 	INIT_LIST_HEAD(&video_info->node);
228 
229 	str = kstrdup(buf, GFP_KERNEL);
230 	p = str;
231 	strsep(&p, ",");
232 	video_info->width = rockchip_get_video_param(&p);
233 	video_info->height = rockchip_get_video_param(&p);
234 	video_info->ishevc = rockchip_get_video_param(&p);
235 	video_info->videoFramerate = rockchip_get_video_param(&p);
236 	video_info->streamBitrate = rockchip_get_video_param(&p);
237 	pr_debug("%c,width=%d,height=%d,ishevc=%d,videoFramerate=%d,streamBitrate=%d\n",
238 		 buf[0],
239 		 video_info->width,
240 		 video_info->height,
241 		 video_info->ishevc,
242 		 video_info->videoFramerate,
243 		 video_info->streamBitrate);
244 	kfree(str);
245 
246 	return video_info;
247 }
248 
rockchip_find_video_info(const char * buf)249 static struct video_info *rockchip_find_video_info(const char *buf)
250 {
251 	struct video_info *info, *video_info;
252 
253 	video_info = rockchip_parse_video_info(buf);
254 
255 	if (!video_info)
256 		return NULL;
257 
258 	mutex_lock(&video_info_mutex);
259 	list_for_each_entry(info, &video_info_list, node) {
260 		if (info->width == video_info->width &&
261 		    info->height == video_info->height &&
262 		    info->ishevc == video_info->ishevc &&
263 		    info->videoFramerate == video_info->videoFramerate &&
264 		    info->streamBitrate == video_info->streamBitrate) {
265 			mutex_unlock(&video_info_mutex);
266 			kfree(video_info);
267 			return info;
268 		}
269 	}
270 
271 	mutex_unlock(&video_info_mutex);
272 	kfree(video_info);
273 
274 	return NULL;
275 }
276 
rockchip_add_video_info(struct video_info * video_info)277 static void rockchip_add_video_info(struct video_info *video_info)
278 {
279 	if (video_info) {
280 		mutex_lock(&video_info_mutex);
281 		list_add(&video_info->node, &video_info_list);
282 		mutex_unlock(&video_info_mutex);
283 	}
284 }
285 
rockchip_del_video_info(struct video_info * video_info)286 static void rockchip_del_video_info(struct video_info *video_info)
287 {
288 	if (video_info) {
289 		mutex_lock(&video_info_mutex);
290 		list_del(&video_info->node);
291 		mutex_unlock(&video_info_mutex);
292 		kfree(video_info);
293 	}
294 }
295 
rockchip_update_video_info(void)296 static void rockchip_update_video_info(void)
297 {
298 	struct video_info *video_info;
299 	unsigned int max_res = 0, max_stream_bitrate = 0, res = 0;
300 	unsigned int max_video_framerate = 0;
301 
302 	mutex_lock(&video_info_mutex);
303 	if (list_empty(&video_info_list)) {
304 		mutex_unlock(&video_info_mutex);
305 		rockchip_clear_system_status(SYS_STATUS_VIDEO);
306 		return;
307 	}
308 
309 	list_for_each_entry(video_info, &video_info_list, node) {
310 		res = video_info->width * video_info->height;
311 		if (res > max_res)
312 			max_res = res;
313 		if (video_info->streamBitrate > max_stream_bitrate)
314 			max_stream_bitrate = video_info->streamBitrate;
315 		if (video_info->videoFramerate > max_video_framerate)
316 			max_video_framerate = video_info->videoFramerate;
317 	}
318 	mutex_unlock(&video_info_mutex);
319 
320 	if (max_res <= VIDEO_1080P_SIZE) {
321 		rockchip_set_system_status(SYS_STATUS_VIDEO_1080P);
322 	} else {
323 		if (max_stream_bitrate == 10)
324 			rockchip_set_system_status(SYS_STATUS_VIDEO_4K_10B);
325 		if (max_video_framerate == 60)
326 			rockchip_set_system_status(SYS_STATUS_VIDEO_4K_60P);
327 		rockchip_set_system_status(SYS_STATUS_VIDEO_4K);
328 	}
329 }
330 
rockchip_update_system_status(const char * buf)331 void rockchip_update_system_status(const char *buf)
332 {
333 	struct video_info *video_info;
334 
335 	if (!buf)
336 		return;
337 
338 	switch (buf[0]) {
339 	case '0':
340 		/* clear video flag */
341 		video_info = rockchip_find_video_info(buf);
342 		if (video_info) {
343 			rockchip_del_video_info(video_info);
344 			rockchip_update_video_info();
345 		}
346 		break;
347 	case '1':
348 		/* set video flag */
349 		video_info = rockchip_parse_video_info(buf);
350 		if (video_info) {
351 			rockchip_add_video_info(video_info);
352 			rockchip_update_video_info();
353 		}
354 		break;
355 	case 'L':
356 		/* clear low power flag */
357 		rockchip_clear_system_status(SYS_STATUS_LOW_POWER);
358 		break;
359 	case 'l':
360 		/* set low power flag */
361 		rockchip_set_system_status(SYS_STATUS_LOW_POWER);
362 		break;
363 	case 'p':
364 		/* set performance flag */
365 		rockchip_set_system_status(SYS_STATUS_PERFORMANCE);
366 		break;
367 	case 'n':
368 		/* clear performance flag */
369 		rockchip_clear_system_status(SYS_STATUS_PERFORMANCE);
370 		break;
371 	case 'S':
372 		/* set video svep flag */
373 		rockchip_set_system_status(SYS_STATUS_VIDEO_SVEP);
374 		break;
375 	case 's':
376 		/* clear video svep flag */
377 		rockchip_clear_system_status(SYS_STATUS_VIDEO_SVEP);
378 		break;
379 	default:
380 		break;
381 	}
382 }
383 EXPORT_SYMBOL(rockchip_update_system_status);
384 
status_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)385 static ssize_t status_show(struct kobject *kobj, struct kobj_attribute *attr,
386 			   char *buf)
387 {
388 	unsigned int status = rockchip_get_system_status();
389 
390 	return sprintf(buf, "0x%x\n", status);
391 }
392 
status_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)393 static ssize_t status_store(struct kobject *kobj, struct kobj_attribute *attr,
394 			    const char *buf, size_t n)
395 {
396 	if (!n)
397 		return -EINVAL;
398 
399 	rockchip_update_system_status(buf);
400 
401 	return n;
402 }
403 
404 static struct system_monitor_attr status =
405 	__ATTR(system_status, 0644, status_show, status_store);
406 
rockchip_get_temp_freq_table(struct device_node * np,char * porp_name,struct temp_freq_table ** freq_table)407 static int rockchip_get_temp_freq_table(struct device_node *np,
408 					char *porp_name,
409 					struct temp_freq_table **freq_table)
410 {
411 	struct temp_freq_table *table;
412 	const struct property *prop;
413 	int count, i;
414 
415 	prop = of_find_property(np, porp_name, NULL);
416 	if (!prop)
417 		return -EINVAL;
418 
419 	if (!prop->value)
420 		return -ENODATA;
421 
422 	count = of_property_count_u32_elems(np, porp_name);
423 	if (count < 0)
424 		return -EINVAL;
425 
426 	if (count % 2)
427 		return -EINVAL;
428 
429 	table = kzalloc(sizeof(*table) * (count / 2 + 1), GFP_KERNEL);
430 	if (!table)
431 		return -ENOMEM;
432 
433 	for (i = 0; i < count / 2; i++) {
434 		of_property_read_u32_index(np, porp_name, 2 * i,
435 					   &table[i].temp);
436 		of_property_read_u32_index(np, porp_name, 2 * i + 1,
437 					   &table[i].freq);
438 	}
439 	table[i].freq = UINT_MAX;
440 	*freq_table = table;
441 
442 	return 0;
443 }
444 
rockchip_get_adjust_volt_table(struct device_node * np,char * porp_name,struct volt_adjust_table ** table)445 static int rockchip_get_adjust_volt_table(struct device_node *np,
446 					  char *porp_name,
447 					  struct volt_adjust_table **table)
448 {
449 	struct volt_adjust_table *volt_table;
450 	const struct property *prop;
451 	int count, i;
452 
453 	prop = of_find_property(np, porp_name, NULL);
454 	if (!prop)
455 		return -EINVAL;
456 
457 	if (!prop->value)
458 		return -ENODATA;
459 
460 	count = of_property_count_u32_elems(np, porp_name);
461 	if (count < 0)
462 		return -EINVAL;
463 
464 	if (count % 3)
465 		return -EINVAL;
466 
467 	volt_table = kzalloc(sizeof(*volt_table) * (count / 3 + 1), GFP_KERNEL);
468 	if (!volt_table)
469 		return -ENOMEM;
470 
471 	for (i = 0; i < count / 3; i++) {
472 		of_property_read_u32_index(np, porp_name, 3 * i,
473 					   &volt_table[i].min);
474 		of_property_read_u32_index(np, porp_name, 3 * i + 1,
475 					   &volt_table[i].max);
476 		of_property_read_u32_index(np, porp_name, 3 * i + 2,
477 					   &volt_table[i].volt);
478 	}
479 	volt_table[i].min = 0;
480 	volt_table[i].max = 0;
481 	volt_table[i].volt = INT_MAX;
482 
483 	*table = volt_table;
484 
485 	return 0;
486 }
487 
rockchip_get_low_temp_volt(struct monitor_dev_info * info,unsigned long rate,int * delta_volt)488 static int rockchip_get_low_temp_volt(struct monitor_dev_info *info,
489 				      unsigned long rate, int *delta_volt)
490 {
491 	int i, ret = -EINVAL;
492 	unsigned int _rate = (unsigned int)(rate / 1000000);
493 
494 	if (!info->low_temp_adjust_table)
495 		return ret;
496 
497 	for (i = 0; info->low_temp_adjust_table[i].volt != INT_MAX; i++) {
498 		if (_rate >= info->low_temp_adjust_table[i].min &&
499 		    _rate <= info->low_temp_adjust_table[i].max) {
500 			*delta_volt = info->low_temp_adjust_table[i].volt;
501 			ret = 0;
502 		}
503 	}
504 
505 	return ret;
506 }
507 
rockchip_init_temp_opp_table(struct monitor_dev_info * info)508 static int rockchip_init_temp_opp_table(struct monitor_dev_info *info)
509 {
510 	struct device *dev = info->dev;
511 	struct opp_table *opp_table;
512 	struct dev_pm_opp *opp;
513 	int delta_volt = 0;
514 	int i = 0, max_count;
515 	unsigned long low_limit = 0, high_limit = 0;
516 	unsigned long low_limit_mem = 0, high_limit_mem = 0;
517 	bool reach_max_volt = false;
518 	bool reach_max_mem_volt = false;
519 	bool reach_high_temp_max_volt = false;
520 	bool reach_high_temp_max_mem_volt = false;
521 
522 	max_count = dev_pm_opp_get_opp_count(dev);
523 	if (max_count <= 0)
524 		return max_count ? max_count : -ENODATA;
525 	info->opp_table = kzalloc(sizeof(*info->opp_table) * max_count,
526 				  GFP_KERNEL);
527 	if (!info->opp_table)
528 		return -ENOMEM;
529 
530 	opp_table = dev_pm_opp_get_opp_table(dev);
531 	if (!opp_table) {
532 		kfree(info->opp_table);
533 		info->opp_table = NULL;
534 		return -ENOMEM;
535 	}
536 	mutex_lock(&opp_table->lock);
537 	list_for_each_entry(opp, &opp_table->opp_list, node) {
538 		if (!opp->available)
539 			continue;
540 		info->opp_table[i].rate = opp->rate;
541 		info->opp_table[i].volt = opp->supplies[0].u_volt;
542 		info->opp_table[i].max_volt = opp->supplies[0].u_volt_max;
543 
544 		if (opp->supplies[0].u_volt <= info->high_temp_max_volt) {
545 			if (!reach_high_temp_max_volt)
546 				high_limit = opp->rate;
547 			if (opp->supplies[0].u_volt == info->high_temp_max_volt)
548 				reach_high_temp_max_volt = true;
549 		}
550 
551 		if (rockchip_get_low_temp_volt(info, opp->rate, &delta_volt))
552 			delta_volt = 0;
553 		if ((opp->supplies[0].u_volt + delta_volt) <= info->max_volt) {
554 			info->opp_table[i].low_temp_volt =
555 				opp->supplies[0].u_volt + delta_volt;
556 			if (info->opp_table[i].low_temp_volt <
557 			    info->low_temp_min_volt)
558 				info->opp_table[i].low_temp_volt =
559 					info->low_temp_min_volt;
560 			if (!reach_max_volt)
561 				low_limit = opp->rate;
562 			if (info->opp_table[i].low_temp_volt == info->max_volt)
563 				reach_max_volt = true;
564 		} else {
565 			info->opp_table[i].low_temp_volt = info->max_volt;
566 		}
567 		if (low_limit && low_limit != opp->rate)
568 			info->low_limit = low_limit;
569 		if (high_limit && high_limit != opp->rate)
570 			info->high_limit = high_limit;
571 
572 		if (opp_table->regulator_count > 1) {
573 			info->opp_table[i].mem_volt = opp->supplies[1].u_volt;
574 			info->opp_table[i].max_mem_volt = opp->supplies[1].u_volt_max;
575 
576 			if (opp->supplies[1].u_volt <= info->high_temp_max_volt) {
577 				if (!reach_high_temp_max_mem_volt)
578 					high_limit_mem = opp->rate;
579 				if (opp->supplies[1].u_volt == info->high_temp_max_volt)
580 					reach_high_temp_max_mem_volt = true;
581 			}
582 
583 			if ((opp->supplies[1].u_volt + delta_volt) <= info->max_volt) {
584 				info->opp_table[i].low_temp_mem_volt =
585 					opp->supplies[1].u_volt + delta_volt;
586 				if (info->opp_table[i].low_temp_mem_volt <
587 				    info->low_temp_min_volt)
588 					info->opp_table[i].low_temp_mem_volt =
589 						info->low_temp_min_volt;
590 				if (!reach_max_mem_volt)
591 					low_limit_mem = opp->rate;
592 				if (info->opp_table[i].low_temp_mem_volt == info->max_volt)
593 					reach_max_mem_volt = true;
594 			} else {
595 				info->opp_table[i].low_temp_mem_volt = info->max_volt;
596 			}
597 
598 			if (low_limit_mem && low_limit_mem != opp->rate) {
599 				if (info->low_limit > low_limit_mem)
600 					info->low_limit = low_limit_mem;
601 			}
602 			if (high_limit_mem && high_limit_mem != opp->rate) {
603 				if (info->high_limit > high_limit_mem)
604 					info->high_limit = high_limit_mem;
605 			}
606 		}
607 
608 		dev_dbg(dev, "rate=%lu, volt=%lu %lu low_temp_volt=%lu %lu\n",
609 			info->opp_table[i].rate, info->opp_table[i].volt,
610 			info->opp_table[i].mem_volt,
611 			info->opp_table[i].low_temp_volt,
612 			info->opp_table[i].low_temp_mem_volt);
613 		i++;
614 	}
615 	mutex_unlock(&opp_table->lock);
616 
617 	dev_pm_opp_put_opp_table(opp_table);
618 
619 	return 0;
620 }
621 
monitor_device_parse_wide_temp_config(struct device_node * np,struct monitor_dev_info * info)622 static int monitor_device_parse_wide_temp_config(struct device_node *np,
623 						 struct monitor_dev_info *info)
624 {
625 	struct device *dev = info->dev;
626 	unsigned long high_temp_max_freq;
627 	int ret = 0;
628 	u32 value;
629 
630 	np = of_parse_phandle(dev->of_node, "operating-points-v2", 0);
631 	if (!np)
632 		return -EINVAL;
633 
634 	if (of_property_read_u32(np, "rockchip,max-volt", &value))
635 		info->max_volt = ULONG_MAX;
636 	else
637 		info->max_volt = value;
638 	of_property_read_u32(np, "rockchip,temp-hysteresis",
639 			     &info->temp_hysteresis);
640 	if (of_property_read_u32(np, "rockchip,low-temp", &info->low_temp))
641 		info->low_temp = INT_MIN;
642 	rockchip_get_adjust_volt_table(np, "rockchip,low-temp-adjust-volt",
643 				       &info->low_temp_adjust_table);
644 	if (!of_property_read_u32(np, "rockchip,low-temp-min-volt", &value))
645 		info->low_temp_min_volt = value;
646 	if (of_property_read_u32(np, "rockchip,high-temp", &info->high_temp))
647 		info->high_temp = INT_MAX;
648 	if (of_property_read_u32(np, "rockchip,high-temp-max-volt",
649 				 &value))
650 		info->high_temp_max_volt = ULONG_MAX;
651 	else
652 		info->high_temp_max_volt = value;
653 	rockchip_init_temp_opp_table(info);
654 	rockchip_get_temp_freq_table(np, "rockchip,temp-freq-table",
655 				     &info->high_limit_table);
656 	if (!info->high_limit_table)
657 		rockchip_get_temp_freq_table(np, "rockchip,high-temp-limit-table",
658 					     &info->high_limit_table);
659 	if (!info->high_limit_table) {
660 		if (!of_property_read_u32(np, "rockchip,high-temp-max-freq",
661 					  &value)) {
662 			high_temp_max_freq = value * 1000;
663 			if (info->high_limit)
664 				info->high_limit = min(high_temp_max_freq,
665 						       info->high_limit);
666 			else
667 				info->high_limit = high_temp_max_freq;
668 		}
669 	} else {
670 		info->high_limit = 0;
671 	}
672 	dev_info(dev, "l=%d h=%d hyst=%d l_limit=%lu h_limit=%lu h_table=%d\n",
673 		 info->low_temp, info->high_temp, info->temp_hysteresis,
674 		 info->low_limit, info->high_limit,
675 		 info->high_limit_table ? true : false);
676 
677 	if ((info->low_temp + info->temp_hysteresis) > info->high_temp) {
678 		dev_err(dev, "Invalid temperature, low=%d high=%d hyst=%d\n",
679 			info->low_temp, info->high_temp,
680 			info->temp_hysteresis);
681 		ret = -EINVAL;
682 		goto err;
683 	}
684 	if (!info->low_temp_adjust_table && !info->low_temp_min_volt &&
685 	    !info->low_limit && !info->high_limit && !info->high_limit_table) {
686 		ret = -EINVAL;
687 		goto err;
688 	}
689 	if (info->low_temp_adjust_table || info->low_temp_min_volt)
690 		info->is_low_temp_enabled = true;
691 
692 	return 0;
693 err:
694 	kfree(info->low_temp_adjust_table);
695 	info->low_temp_adjust_table = NULL;
696 	kfree(info->opp_table);
697 	info->opp_table = NULL;
698 
699 	return ret;
700 }
701 
monitor_device_parse_status_config(struct device_node * np,struct monitor_dev_info * info)702 static int monitor_device_parse_status_config(struct device_node *np,
703 					      struct monitor_dev_info *info)
704 {
705 	int ret;
706 
707 	ret = of_property_read_u32(np, "rockchip,video-4k-freq",
708 				   &info->video_4k_freq);
709 	ret &= of_property_read_u32(np, "rockchip,reboot-freq",
710 				    &info->reboot_freq);
711 	if (info->devp->type == MONITOR_TYPE_CPU) {
712 		if (!info->reboot_freq) {
713 			info->reboot_freq = CPU_REBOOT_FREQ;
714 			ret = 0;
715 		}
716 	}
717 
718 	return ret;
719 }
720 
monitor_device_parse_early_min_volt(struct device_node * np,struct monitor_dev_info * info)721 static int monitor_device_parse_early_min_volt(struct device_node *np,
722 					       struct monitor_dev_info *info)
723 {
724 	return of_property_read_u32(np, "rockchip,early-min-microvolt",
725 				    &info->early_min_volt);
726 }
727 
monitor_device_parse_read_margin(struct device_node * np,struct monitor_dev_info * info)728 static int monitor_device_parse_read_margin(struct device_node *np,
729 					    struct monitor_dev_info *info)
730 {
731 	if (of_property_read_bool(np, "volt-mem-read-margin"))
732 		return 0;
733 	return -EINVAL;
734 }
735 
monitor_device_parse_scmi_clk(struct device_node * np,struct monitor_dev_info * info)736 static int monitor_device_parse_scmi_clk(struct device_node *np,
737 					 struct monitor_dev_info *info)
738 {
739 	struct clk *clk;
740 
741 	clk = clk_get(info->dev, NULL);
742 	if (strstr(__clk_get_name(clk), "scmi"))
743 		return 0;
744 	return -EINVAL;
745 }
746 
monitor_device_parse_dt(struct device * dev,struct monitor_dev_info * info)747 static int monitor_device_parse_dt(struct device *dev,
748 				   struct monitor_dev_info *info)
749 {
750 	struct device_node *np;
751 	int ret;
752 
753 	np = of_parse_phandle(dev->of_node, "operating-points-v2", 0);
754 	if (!np)
755 		return -EINVAL;
756 
757 	of_property_read_u32(np, "rockchip,init-freq", &info->init_freq);
758 
759 	ret = monitor_device_parse_wide_temp_config(np, info);
760 	ret &= monitor_device_parse_status_config(np, info);
761 	ret &= monitor_device_parse_early_min_volt(np, info);
762 	ret &= monitor_device_parse_read_margin(np, info);
763 	ret &= monitor_device_parse_scmi_clk(np, info);
764 
765 	of_node_put(np);
766 
767 	return ret;
768 }
769 
rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info * info,bool is_low)770 int rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info *info,
771 					 bool is_low)
772 {
773 	if (!info->low_limit)
774 		return 0;
775 
776 	if (!freq_qos_request_active(&info->max_temp_freq_req))
777 		return 0;
778 
779 	if (is_low)
780 		freq_qos_update_request(&info->max_temp_freq_req,
781 					info->low_limit / 1000);
782 	else
783 		freq_qos_update_request(&info->max_temp_freq_req,
784 					FREQ_QOS_MAX_DEFAULT_VALUE);
785 
786 	return 0;
787 }
788 EXPORT_SYMBOL(rockchip_monitor_cpu_low_temp_adjust);
789 
rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info * info,bool is_high)790 int rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info *info,
791 					  bool is_high)
792 {
793 	if (!info->high_limit)
794 		return 0;
795 
796 	if (!freq_qos_request_active(&info->max_temp_freq_req))
797 		return 0;
798 
799 	if (info->high_limit_table) {
800 		freq_qos_update_request(&info->max_temp_freq_req,
801 					info->high_limit / 1000);
802 		return 0;
803 	}
804 
805 	if (is_high)
806 		freq_qos_update_request(&info->max_temp_freq_req,
807 					info->high_limit / 1000);
808 	else
809 		freq_qos_update_request(&info->max_temp_freq_req,
810 					FREQ_QOS_MAX_DEFAULT_VALUE);
811 
812 	return 0;
813 }
814 EXPORT_SYMBOL(rockchip_monitor_cpu_high_temp_adjust);
815 
rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info * info,bool is_low)816 int rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info *info,
817 					 bool is_low)
818 {
819 	if (!dev_pm_qos_request_active(&info->dev_max_freq_req))
820 		return 0;
821 
822 	if (!info->low_limit)
823 		return 0;
824 
825 	if (is_low)
826 		dev_pm_qos_update_request(&info->dev_max_freq_req,
827 					  info->low_limit / 1000);
828 	else
829 		dev_pm_qos_update_request(&info->dev_max_freq_req,
830 					  PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
831 
832 	return 0;
833 }
834 EXPORT_SYMBOL(rockchip_monitor_dev_low_temp_adjust);
835 
rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info * info,bool is_high)836 int rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info *info,
837 					  bool is_high)
838 {
839 	if (!dev_pm_qos_request_active(&info->dev_max_freq_req))
840 		return 0;
841 
842 	if (!info->high_limit)
843 		return 0;
844 
845 	if (info->high_limit_table) {
846 		dev_pm_qos_update_request(&info->dev_max_freq_req,
847 					  info->high_limit / 1000);
848 		return 0;
849 	}
850 
851 	if (is_high)
852 		dev_pm_qos_update_request(&info->dev_max_freq_req,
853 					  info->high_limit / 1000);
854 	else
855 		dev_pm_qos_update_request(&info->dev_max_freq_req,
856 					  PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
857 
858 	return 0;
859 }
860 EXPORT_SYMBOL(rockchip_monitor_dev_high_temp_adjust);
861 
rockchip_adjust_low_temp_opp_volt(struct monitor_dev_info * info,bool is_low_temp)862 static int rockchip_adjust_low_temp_opp_volt(struct monitor_dev_info *info,
863 					     bool is_low_temp)
864 {
865 	struct device *dev = info->dev;
866 	struct opp_table *opp_table;
867 	struct dev_pm_opp *opp;
868 	int i = 0;
869 
870 	opp_table = dev_pm_opp_get_opp_table(dev);
871 	if (!opp_table)
872 		return -ENOMEM;
873 
874 	mutex_lock(&opp_table->lock);
875 	list_for_each_entry(opp, &opp_table->opp_list, node) {
876 		if (!opp->available)
877 			continue;
878 		if (is_low_temp) {
879 			if (opp->supplies[0].u_volt_max <
880 			    info->opp_table[i].low_temp_volt)
881 				opp->supplies[0].u_volt_max =
882 					info->opp_table[i].low_temp_volt;
883 			opp->supplies[0].u_volt =
884 				info->opp_table[i].low_temp_volt;
885 			opp->supplies[0].u_volt_min = opp->supplies[0].u_volt;
886 			if (opp_table->regulator_count > 1) {
887 				if (opp->supplies[1].u_volt_max <
888 				    info->opp_table[i].low_temp_mem_volt)
889 					opp->supplies[1].u_volt_max =
890 						info->opp_table[i].low_temp_mem_volt;
891 				opp->supplies[1].u_volt =
892 					info->opp_table[i].low_temp_mem_volt;
893 				opp->supplies[1].u_volt_min =
894 					opp->supplies[1].u_volt;
895 			}
896 		} else {
897 			opp->supplies[0].u_volt_min = info->opp_table[i].volt;
898 			opp->supplies[0].u_volt = opp->supplies[0].u_volt_min;
899 			opp->supplies[0].u_volt_max =
900 				info->opp_table[i].max_volt;
901 			if (opp_table->regulator_count > 1) {
902 				opp->supplies[1].u_volt_min =
903 					info->opp_table[i].mem_volt;
904 				opp->supplies[1].u_volt =
905 					opp->supplies[1].u_volt_min;
906 				opp->supplies[1].u_volt_max =
907 					info->opp_table[i].max_mem_volt;
908 			}
909 		}
910 		i++;
911 	}
912 	mutex_unlock(&opp_table->lock);
913 
914 	dev_pm_opp_put_opp_table(opp_table);
915 
916 	return 0;
917 }
918 
rockchip_low_temp_adjust(struct monitor_dev_info * info,bool is_low)919 static void rockchip_low_temp_adjust(struct monitor_dev_info *info,
920 				     bool is_low)
921 {
922 	struct monitor_dev_profile *devp = info->devp;
923 	int ret = 0;
924 
925 	dev_dbg(info->dev, "low_temp %d\n", is_low);
926 
927 	if (info->opp_table)
928 		rockchip_adjust_low_temp_opp_volt(info, is_low);
929 
930 	if (devp->low_temp_adjust)
931 		ret = devp->low_temp_adjust(info, is_low);
932 	if (!ret)
933 		info->is_low_temp = is_low;
934 
935 	if (devp->update_volt)
936 		devp->update_volt(info);
937 }
938 
rockchip_high_temp_adjust(struct monitor_dev_info * info,bool is_high)939 static void rockchip_high_temp_adjust(struct monitor_dev_info *info,
940 				      bool is_high)
941 {
942 	struct monitor_dev_profile *devp = info->devp;
943 	int ret = 0;
944 
945 	if (!devp->high_temp_adjust)
946 		return;
947 
948 	if (info->high_limit_table) {
949 		devp->high_temp_adjust(info, is_high);
950 	} else {
951 		dev_dbg(info->dev, "high_temp %d\n", is_high);
952 		ret = devp->high_temp_adjust(info, is_high);
953 		if (!ret)
954 			info->is_high_temp = is_high;
955 	}
956 }
957 
rockchip_monitor_suspend_low_temp_adjust(int cpu)958 int rockchip_monitor_suspend_low_temp_adjust(int cpu)
959 {
960 	struct monitor_dev_info *info = NULL, *tmp;
961 
962 	list_for_each_entry(tmp, &monitor_dev_list, node) {
963 		if (tmp->devp->type != MONITOR_TYPE_CPU)
964 			continue;
965 		if (cpumask_test_cpu(cpu, &tmp->devp->allowed_cpus)) {
966 			info = tmp;
967 			break;
968 		}
969 	}
970 
971 	if (!info || !info->is_low_temp_enabled)
972 		return 0;
973 
974 	if (info->high_limit_table) {
975 		info->high_limit = 0;
976 		rockchip_high_temp_adjust(info, true);
977 	} else if (info->is_high_temp) {
978 		rockchip_high_temp_adjust(info, false);
979 	}
980 	if (!info->is_low_temp)
981 		rockchip_low_temp_adjust(info, true);
982 
983 	return 0;
984 }
985 EXPORT_SYMBOL(rockchip_monitor_suspend_low_temp_adjust);
986 
987 static int
rockchip_system_monitor_wide_temp_adjust(struct monitor_dev_info * info,int temp)988 rockchip_system_monitor_wide_temp_adjust(struct monitor_dev_info *info,
989 					 int temp)
990 {
991 	unsigned long target_freq = 0;
992 	int i;
993 
994 	if (temp < info->low_temp) {
995 		if (!info->is_low_temp)
996 			rockchip_low_temp_adjust(info, true);
997 	} else if (temp > (info->low_temp + info->temp_hysteresis)) {
998 		if (info->is_low_temp)
999 			rockchip_low_temp_adjust(info, false);
1000 	}
1001 
1002 	if (info->high_limit_table) {
1003 		for (i = 0; info->high_limit_table[i].freq != UINT_MAX; i++) {
1004 			if (temp > info->high_limit_table[i].temp)
1005 				target_freq =
1006 					info->high_limit_table[i].freq * 1000;
1007 		}
1008 		if (target_freq != info->high_limit) {
1009 			info->high_limit = target_freq;
1010 			rockchip_high_temp_adjust(info, true);
1011 		}
1012 	} else {
1013 		if (temp > info->high_temp) {
1014 			if (!info->is_high_temp)
1015 				rockchip_high_temp_adjust(info, true);
1016 		} else if (temp < (info->high_temp - info->temp_hysteresis)) {
1017 			if (info->is_high_temp)
1018 				rockchip_high_temp_adjust(info, false);
1019 		}
1020 	}
1021 
1022 	return 0;
1023 }
1024 
1025 static void
rockchip_system_monitor_wide_temp_init(struct monitor_dev_info * info)1026 rockchip_system_monitor_wide_temp_init(struct monitor_dev_info *info)
1027 {
1028 	int ret, temp;
1029 
1030 	if (!info->opp_table)
1031 		return;
1032 
1033 	/*
1034 	 * set the init state to low temperature that the voltage will be enough
1035 	 * when cpu up at low temperature.
1036 	 */
1037 	if (!info->is_low_temp) {
1038 		if (info->opp_table)
1039 			rockchip_adjust_low_temp_opp_volt(info, true);
1040 		info->is_low_temp = true;
1041 	}
1042 
1043 	ret = thermal_zone_get_temp(system_monitor->tz, &temp);
1044 	if (ret || temp == THERMAL_TEMP_INVALID) {
1045 		dev_err(info->dev,
1046 			"failed to read out thermal zone (%d)\n", ret);
1047 		return;
1048 	}
1049 
1050 	if (temp > info->high_temp) {
1051 		if (info->opp_table)
1052 			rockchip_adjust_low_temp_opp_volt(info, false);
1053 		info->is_low_temp = false;
1054 		info->is_high_temp = true;
1055 	} else if (temp > (info->low_temp + info->temp_hysteresis)) {
1056 		if (info->opp_table)
1057 			rockchip_adjust_low_temp_opp_volt(info, false);
1058 		info->is_low_temp = false;
1059 	}
1060 }
1061 
get_rdev_name(struct regulator_dev * rdev)1062 static const char *get_rdev_name(struct regulator_dev *rdev)
1063 {
1064 	if (rdev->constraints && rdev->constraints->name)
1065 		return rdev->constraints->name;
1066 	else if (rdev->desc->name)
1067 		return rdev->desc->name;
1068 	else
1069 		return "";
1070 }
1071 
1072 static void
rockchip_system_monitor_early_regulator_init(struct monitor_dev_info * info)1073 rockchip_system_monitor_early_regulator_init(struct monitor_dev_info *info)
1074 {
1075 	struct regulator *reg;
1076 	struct regulator_dev *rdev;
1077 
1078 	if (!info->early_min_volt || !info->regulators)
1079 		return;
1080 
1081 	rdev = info->regulators[0]->rdev;
1082 	reg = regulator_get(NULL, get_rdev_name(rdev));
1083 	if (!IS_ERR_OR_NULL(reg)) {
1084 		info->early_reg = reg;
1085 		reg->voltage[PM_SUSPEND_ON].min_uV = info->early_min_volt;
1086 		reg->voltage[PM_SUSPEND_ON].max_uV = rdev->constraints->max_uV;
1087 	}
1088 }
1089 
1090 static int
rockchip_system_monitor_freq_qos_requset(struct monitor_dev_info * info)1091 rockchip_system_monitor_freq_qos_requset(struct monitor_dev_info *info)
1092 {
1093 	struct devfreq *devfreq;
1094 	struct cpufreq_policy *policy;
1095 	int max_default_value = FREQ_QOS_MAX_DEFAULT_VALUE;
1096 	int ret;
1097 
1098 	if (!info->devp->data)
1099 		return 0;
1100 
1101 	if (info->is_low_temp && info->low_limit)
1102 		max_default_value = info->low_limit / 1000;
1103 	else if (info->is_high_temp && info->high_limit)
1104 		max_default_value = info->high_limit / 1000;
1105 
1106 	if (info->devp->type == MONITOR_TYPE_CPU) {
1107 		policy = (struct cpufreq_policy *)info->devp->data;
1108 		ret = freq_qos_add_request(&policy->constraints,
1109 					   &info->max_temp_freq_req,
1110 					   FREQ_QOS_MAX,
1111 					   max_default_value);
1112 		if (ret < 0) {
1113 			dev_info(info->dev,
1114 				 "failed to add temp freq constraint\n");
1115 			return ret;
1116 		}
1117 		ret = freq_qos_add_request(&policy->constraints,
1118 					   &info->min_sta_freq_req,
1119 					   FREQ_QOS_MIN,
1120 					   FREQ_QOS_MIN_DEFAULT_VALUE);
1121 		if (ret < 0) {
1122 			dev_info(info->dev,
1123 				 "failed to add sta freq constraint\n");
1124 			freq_qos_remove_request(&info->max_temp_freq_req);
1125 			return ret;
1126 		}
1127 		ret = freq_qos_add_request(&policy->constraints,
1128 					   &info->max_sta_freq_req,
1129 					   FREQ_QOS_MAX,
1130 					   FREQ_QOS_MAX_DEFAULT_VALUE);
1131 		if (ret < 0) {
1132 			dev_info(info->dev,
1133 				 "failed to add sta freq constraint\n");
1134 			freq_qos_remove_request(&info->max_temp_freq_req);
1135 			freq_qos_remove_request(&info->min_sta_freq_req);
1136 			return ret;
1137 		}
1138 	} else if (info->devp->type == MONITOR_TYPE_DEV) {
1139 		devfreq = (struct devfreq *)info->devp->data;
1140 		ret = dev_pm_qos_add_request(devfreq->dev.parent,
1141 					     &info->dev_max_freq_req,
1142 					     DEV_PM_QOS_MAX_FREQUENCY,
1143 					     max_default_value);
1144 		if (ret < 0) {
1145 			dev_info(info->dev, "failed to add freq constraint\n");
1146 			return ret;
1147 		}
1148 	}
1149 
1150 	return 0;
1151 }
1152 
rockchip_system_monitor_parse_supplies(struct device * dev,struct monitor_dev_info * info)1153 static int rockchip_system_monitor_parse_supplies(struct device *dev,
1154 						  struct monitor_dev_info *info)
1155 {
1156 	struct opp_table *opp_table;
1157 	struct dev_pm_set_opp_data *data;
1158 	int len, count;
1159 
1160 	opp_table = dev_pm_opp_get_opp_table(dev);
1161 	if (IS_ERR(opp_table))
1162 		return PTR_ERR(opp_table);
1163 
1164 	if (opp_table->clk)
1165 		info->clk = opp_table->clk;
1166 	if (opp_table->regulators)
1167 		info->regulators = opp_table->regulators;
1168 	info->regulator_count = opp_table->regulator_count;
1169 
1170 	if (opp_table->regulators && info->devp->set_opp) {
1171 		count = opp_table->regulator_count;
1172 		/* space for set_opp_data */
1173 		len = sizeof(*data);
1174 		/* space for old_opp.supplies and new_opp.supplies */
1175 		len += 2 * sizeof(struct dev_pm_opp_supply) * count;
1176 		data = kzalloc(len, GFP_KERNEL);
1177 		if (!data)
1178 			return -ENOMEM;
1179 		data->old_opp.supplies = (void *)(data + 1);
1180 		data->new_opp.supplies = data->old_opp.supplies + count;
1181 		info->set_opp_data = data;
1182 	}
1183 
1184 	dev_pm_opp_put_opp_table(opp_table);
1185 
1186 	return 0;
1187 }
1188 
rockchip_monitor_volt_adjust_lock(struct monitor_dev_info * info)1189 void rockchip_monitor_volt_adjust_lock(struct monitor_dev_info *info)
1190 {
1191 	if (info)
1192 		mutex_lock(&info->volt_adjust_mutex);
1193 }
1194 EXPORT_SYMBOL(rockchip_monitor_volt_adjust_lock);
1195 
rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info * info)1196 void rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info *info)
1197 {
1198 	if (info)
1199 		mutex_unlock(&info->volt_adjust_mutex);
1200 }
1201 EXPORT_SYMBOL(rockchip_monitor_volt_adjust_unlock);
1202 
rockchip_monitor_enable_opp_clk(struct device * dev,struct rockchip_opp_info * opp_info)1203 static int rockchip_monitor_enable_opp_clk(struct device *dev,
1204 					   struct rockchip_opp_info *opp_info)
1205 {
1206 	int ret = 0;
1207 
1208 	if (!opp_info)
1209 		return 0;
1210 
1211 	ret = clk_bulk_prepare_enable(opp_info->num_clks, opp_info->clks);
1212 	if (ret) {
1213 		dev_err(dev, "failed to enable opp clks\n");
1214 		return ret;
1215 	}
1216 
1217 	return 0;
1218 }
1219 
rockchip_monitor_disable_opp_clk(struct device * dev,struct rockchip_opp_info * opp_info)1220 static void rockchip_monitor_disable_opp_clk(struct device *dev,
1221 					     struct rockchip_opp_info *opp_info)
1222 {
1223 	if (!opp_info)
1224 		return;
1225 
1226 	clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks);
1227 }
1228 
rockchip_monitor_set_opp(struct monitor_dev_info * info,unsigned long old_freq,unsigned long freq,struct dev_pm_opp_supply * old_supply,struct dev_pm_opp_supply * new_supply)1229 static int rockchip_monitor_set_opp(struct monitor_dev_info *info,
1230 				    unsigned long old_freq,
1231 				    unsigned long freq,
1232 				    struct dev_pm_opp_supply *old_supply,
1233 				    struct dev_pm_opp_supply *new_supply)
1234 {
1235 	struct dev_pm_set_opp_data *data;
1236 	int size;
1237 
1238 	data = info->set_opp_data;
1239 	data->regulators = info->regulators;
1240 	data->regulator_count = info->regulator_count;
1241 	data->clk = info->clk;
1242 	data->dev = info->dev;
1243 
1244 	data->old_opp.rate = old_freq;
1245 	size = sizeof(*old_supply) * info->regulator_count;
1246 	if (!old_supply)
1247 		memset(data->old_opp.supplies, 0, size);
1248 	else
1249 		memcpy(data->old_opp.supplies, old_supply, size);
1250 
1251 	data->new_opp.rate = freq;
1252 	memcpy(data->new_opp.supplies, new_supply, size);
1253 
1254 	return info->devp->set_opp(data);
1255 }
1256 
rockchip_monitor_check_rate_volt(struct monitor_dev_info * info)1257 int rockchip_monitor_check_rate_volt(struct monitor_dev_info *info)
1258 {
1259 	struct device *dev = info->dev;
1260 	struct regulator *vdd_reg = NULL;
1261 	struct regulator *mem_reg = NULL;
1262 	struct rockchip_opp_info *opp_info = info->devp->opp_info;
1263 	struct dev_pm_opp *opp;
1264 	unsigned long old_rate, new_rate, new_volt, new_mem_volt;
1265 	int old_volt, old_mem_volt;
1266 	u32 target_rm = UINT_MAX;
1267 	bool is_set_clk = true;
1268 	bool is_set_rm = false;
1269 	int ret = 0;
1270 
1271 	if (!info->regulators || !info->clk)
1272 		return 0;
1273 
1274 	mutex_lock(&info->volt_adjust_mutex);
1275 
1276 	vdd_reg = info->regulators[0];
1277 	old_rate = clk_get_rate(info->clk);
1278 	old_volt = regulator_get_voltage(vdd_reg);
1279 	if (info->regulator_count > 1) {
1280 		mem_reg = info->regulators[1];
1281 		old_mem_volt = regulator_get_voltage(mem_reg);
1282 	}
1283 
1284 	if (info->init_freq) {
1285 		new_rate = info->init_freq * 1000;
1286 		info->init_freq = 0;
1287 	} else {
1288 		new_rate = old_rate;
1289 	}
1290 	opp = dev_pm_opp_find_freq_ceil(dev, &new_rate);
1291 	if (IS_ERR(opp)) {
1292 		opp = dev_pm_opp_find_freq_floor(dev, &new_rate);
1293 		if (IS_ERR(opp)) {
1294 			ret = PTR_ERR(opp);
1295 			goto unlock;
1296 		}
1297 	}
1298 	new_volt = opp->supplies[0].u_volt;
1299 	if (info->regulator_count > 1)
1300 		new_mem_volt = opp->supplies[1].u_volt;
1301 	dev_pm_opp_put(opp);
1302 
1303 	if (old_rate == new_rate) {
1304 		if (info->regulator_count > 1) {
1305 			if (old_volt == new_volt &&
1306 			    new_mem_volt == old_mem_volt)
1307 				goto unlock;
1308 		} else if (old_volt == new_volt) {
1309 			goto unlock;
1310 		}
1311 	}
1312 	if (!new_volt || (info->regulator_count > 1 && !new_mem_volt))
1313 		goto unlock;
1314 
1315 	if (info->devp->set_opp) {
1316 		ret = rockchip_monitor_set_opp(info, old_rate, new_rate,
1317 					       NULL, opp->supplies);
1318 		goto unlock;
1319 	}
1320 
1321 	if (opp_info && opp_info->data && opp_info->data->set_read_margin) {
1322 		is_set_rm = true;
1323 		if (info->devp->type == MONITOR_TYPE_DEV) {
1324 			if (!pm_runtime_active(dev)) {
1325 				is_set_rm = false;
1326 				if (opp_info->scmi_clk)
1327 					is_set_clk = false;
1328 			}
1329 		}
1330 	}
1331 	rockchip_monitor_enable_opp_clk(dev, opp_info);
1332 	rockchip_get_read_margin(dev, opp_info, new_volt, &target_rm);
1333 
1334 	dev_dbg(dev, "%s: %lu Hz --> %lu Hz\n", __func__, old_rate, new_rate);
1335 	if (new_rate >= old_rate) {
1336 		rockchip_set_intermediate_rate(dev, opp_info, info->clk,
1337 					       old_rate, new_rate,
1338 					       true, is_set_clk);
1339 		if (info->regulator_count > 1) {
1340 			ret = regulator_set_voltage(mem_reg, new_mem_volt,
1341 						    INT_MAX);
1342 			if (ret) {
1343 				dev_err(dev, "%s: failed to set volt: %lu\n",
1344 					__func__, new_mem_volt);
1345 				goto restore_voltage;
1346 			}
1347 		}
1348 		ret = regulator_set_voltage(vdd_reg, new_volt, INT_MAX);
1349 		if (ret) {
1350 			dev_err(dev, "%s: failed to set volt: %lu\n",
1351 				__func__, new_volt);
1352 			goto restore_voltage;
1353 		}
1354 		rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm);
1355 		if (is_set_clk && clk_set_rate(info->clk, new_rate)) {
1356 			dev_err(dev, "%s: failed to set clock rate: %lu\n",
1357 				__func__, new_rate);
1358 			goto restore_rm;
1359 		}
1360 	} else {
1361 		rockchip_set_intermediate_rate(dev, opp_info, info->clk,
1362 					       old_rate, new_rate,
1363 					       false, is_set_clk);
1364 		rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm);
1365 		if (is_set_clk && clk_set_rate(info->clk, new_rate)) {
1366 			dev_err(dev, "%s: failed to set clock rate: %lu\n",
1367 				__func__, new_rate);
1368 			goto restore_rm;
1369 		}
1370 		ret = regulator_set_voltage(vdd_reg, new_volt,
1371 					    INT_MAX);
1372 		if (ret) {
1373 			dev_err(dev, "%s: failed to set volt: %lu\n",
1374 				__func__, new_volt);
1375 			goto restore_freq;
1376 		}
1377 		if (info->regulator_count > 1) {
1378 			ret = regulator_set_voltage(mem_reg, new_mem_volt,
1379 						    INT_MAX);
1380 			if (ret) {
1381 				dev_err(dev, "%s: failed to set volt: %lu\n",
1382 					__func__, new_mem_volt);
1383 				goto restore_freq;
1384 			}
1385 		}
1386 	}
1387 	goto disable_clk;
1388 
1389 restore_freq:
1390 	if (is_set_clk && clk_set_rate(info->clk, old_rate))
1391 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
1392 			__func__, old_rate);
1393 restore_rm:
1394 	rockchip_get_read_margin(dev, opp_info, old_volt, &target_rm);
1395 	rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm);
1396 restore_voltage:
1397 	if (info->regulator_count > 1)
1398 		regulator_set_voltage(mem_reg, old_mem_volt, INT_MAX);
1399 	regulator_set_voltage(vdd_reg, old_volt, INT_MAX);
1400 disable_clk:
1401 	rockchip_monitor_disable_opp_clk(dev, opp_info);
1402 unlock:
1403 	mutex_unlock(&info->volt_adjust_mutex);
1404 
1405 	return ret;
1406 }
1407 EXPORT_SYMBOL(rockchip_monitor_check_rate_volt);
1408 
1409 struct monitor_dev_info *
rockchip_system_monitor_register(struct device * dev,struct monitor_dev_profile * devp)1410 rockchip_system_monitor_register(struct device *dev,
1411 				 struct monitor_dev_profile *devp)
1412 {
1413 	struct monitor_dev_info *info;
1414 
1415 	if (!system_monitor)
1416 		return ERR_PTR(-ENOMEM);
1417 
1418 	if (!devp)
1419 		return ERR_PTR(-EINVAL);
1420 
1421 	info = kzalloc(sizeof(*info), GFP_KERNEL);
1422 	if (!info)
1423 		return ERR_PTR(-ENOMEM);
1424 	info->dev = dev;
1425 	info->devp = devp;
1426 
1427 	mutex_init(&info->volt_adjust_mutex);
1428 
1429 	rockchip_system_monitor_parse_supplies(dev, info);
1430 	if (monitor_device_parse_dt(dev, info)) {
1431 		rockchip_monitor_check_rate_volt(info);
1432 		devp->is_checked = true;
1433 		kfree(info->set_opp_data);
1434 		kfree(info);
1435 		return ERR_PTR(-EINVAL);
1436 	}
1437 
1438 	rockchip_system_monitor_early_regulator_init(info);
1439 	rockchip_system_monitor_wide_temp_init(info);
1440 	rockchip_monitor_check_rate_volt(info);
1441 	devp->is_checked = true;
1442 	rockchip_system_monitor_freq_qos_requset(info);
1443 
1444 	down_write(&mdev_list_sem);
1445 	list_add(&info->node, &monitor_dev_list);
1446 	up_write(&mdev_list_sem);
1447 
1448 	return info;
1449 }
1450 EXPORT_SYMBOL(rockchip_system_monitor_register);
1451 
rockchip_system_monitor_unregister(struct monitor_dev_info * info)1452 void rockchip_system_monitor_unregister(struct monitor_dev_info *info)
1453 {
1454 	if (!info)
1455 		return;
1456 
1457 	down_write(&mdev_list_sem);
1458 	list_del(&info->node);
1459 	up_write(&mdev_list_sem);
1460 
1461 	if (info->devp->type == MONITOR_TYPE_CPU) {
1462 		if (freq_qos_request_active(&info->max_temp_freq_req))
1463 			freq_qos_remove_request(&info->max_temp_freq_req);
1464 		if (freq_qos_request_active(&info->min_sta_freq_req))
1465 			freq_qos_remove_request(&info->min_sta_freq_req);
1466 		if (freq_qos_request_active(&info->max_sta_freq_req))
1467 			freq_qos_remove_request(&info->max_sta_freq_req);
1468 	} else {
1469 		if (dev_pm_qos_request_active(&info->dev_max_freq_req))
1470 			dev_pm_qos_remove_request(&info->dev_max_freq_req);
1471 	}
1472 
1473 	kfree(info->low_temp_adjust_table);
1474 	kfree(info->opp_table);
1475 	kfree(info->set_opp_data);
1476 	kfree(info);
1477 }
1478 EXPORT_SYMBOL(rockchip_system_monitor_unregister);
1479 
rockchip_system_monitor_register_notifier(struct notifier_block * nb)1480 int rockchip_system_monitor_register_notifier(struct notifier_block *nb)
1481 {
1482 	return blocking_notifier_chain_register(&system_monitor_notifier_list, nb);
1483 }
1484 EXPORT_SYMBOL(rockchip_system_monitor_register_notifier);
1485 
rockchip_system_monitor_unregister_notifier(struct notifier_block * nb)1486 void rockchip_system_monitor_unregister_notifier(struct notifier_block *nb)
1487 {
1488 	blocking_notifier_chain_unregister(&system_monitor_notifier_list, nb);
1489 }
1490 EXPORT_SYMBOL(rockchip_system_monitor_unregister_notifier);
1491 
rockchip_system_monitor_temp_notify(int temp)1492 static int rockchip_system_monitor_temp_notify(int temp)
1493 {
1494 	struct system_monitor_event_data event_data;
1495 	int ret;
1496 
1497 	event_data.temp = temp;
1498 	ret = blocking_notifier_call_chain(&system_monitor_notifier_list,
1499 					   SYSTEM_MONITOR_CHANGE_TEMP,
1500 					   (void *)&event_data);
1501 
1502 	return notifier_to_errno(ret);
1503 }
1504 
notify_dummy(struct thermal_zone_device * tz,int trip)1505 static int notify_dummy(struct thermal_zone_device *tz, int trip)
1506 {
1507 	return 0;
1508 }
1509 
1510 static struct thermal_governor thermal_gov_dummy = {
1511 	.name		= "dummy",
1512 	.throttle	= notify_dummy,
1513 };
1514 
rockchip_system_monitor_parse_dt(struct system_monitor * monitor)1515 static int rockchip_system_monitor_parse_dt(struct system_monitor *monitor)
1516 {
1517 	struct device_node *np = monitor->dev->of_node;
1518 	const char *tz_name, *buf = NULL;
1519 
1520 	if (of_property_read_string(np, "rockchip,video-4k-offline-cpus", &buf))
1521 		cpumask_clear(&monitor->video_4k_offline_cpus);
1522 	else
1523 		cpulist_parse(buf, &monitor->video_4k_offline_cpus);
1524 
1525 	if (of_property_read_string(np, "rockchip,thermal-zone", &tz_name))
1526 		goto out;
1527 	monitor->tz = thermal_zone_get_zone_by_name(tz_name);
1528 	if (IS_ERR(monitor->tz)) {
1529 		monitor->tz = NULL;
1530 		goto out;
1531 	}
1532 	if (of_property_read_u32(np, "rockchip,polling-delay",
1533 				 &monitor->delay))
1534 		monitor->delay = THERMAL_POLLING_DELAY;
1535 
1536 	if (of_property_read_string(np, "rockchip,temp-offline-cpus",
1537 				    &buf))
1538 		cpumask_clear(&system_monitor->temp_offline_cpus);
1539 	else
1540 		cpulist_parse(buf, &system_monitor->temp_offline_cpus);
1541 
1542 	if (of_property_read_u32(np, "rockchip,offline-cpu-temp",
1543 				 &system_monitor->offline_cpus_temp))
1544 		system_monitor->offline_cpus_temp = INT_MAX;
1545 	of_property_read_u32(np, "rockchip,temp-hysteresis",
1546 			     &system_monitor->temp_hysteresis);
1547 
1548 	if (of_find_property(np, "rockchip,thermal-governor-dummy", NULL)) {
1549 		if (monitor->tz->governor->unbind_from_tz)
1550 			monitor->tz->governor->unbind_from_tz(monitor->tz);
1551 		monitor->tz->governor = &thermal_gov_dummy;
1552 	}
1553 
1554 out:
1555 	return 0;
1556 }
1557 
rockchip_system_monitor_cpu_on_off(void)1558 static void rockchip_system_monitor_cpu_on_off(void)
1559 {
1560 #ifdef CONFIG_HOTPLUG_CPU
1561 	struct cpumask online_cpus, offline_cpus;
1562 	unsigned int cpu;
1563 
1564 	mutex_lock(&cpu_on_off_mutex);
1565 
1566 	cpumask_clear(&offline_cpus);
1567 	if (system_monitor->is_temp_offline) {
1568 		cpumask_or(&offline_cpus, &system_monitor->status_offline_cpus,
1569 			   &system_monitor->temp_offline_cpus);
1570 	} else {
1571 		cpumask_copy(&offline_cpus,
1572 			     &system_monitor->status_offline_cpus);
1573 	}
1574 	if (cpumask_equal(&offline_cpus, &system_monitor->offline_cpus))
1575 		goto out;
1576 	cpumask_copy(&system_monitor->offline_cpus, &offline_cpus);
1577 	for_each_cpu(cpu, &system_monitor->offline_cpus) {
1578 		if (cpu_online(cpu))
1579 			remove_cpu(cpu);
1580 	}
1581 
1582 	cpumask_clear(&online_cpus);
1583 	cpumask_andnot(&online_cpus, cpu_possible_mask,
1584 		       &system_monitor->offline_cpus);
1585 	cpumask_xor(&online_cpus, cpu_online_mask, &online_cpus);
1586 	if (cpumask_empty(&online_cpus))
1587 		goto out;
1588 	for_each_cpu(cpu, &online_cpus)
1589 		add_cpu(cpu);
1590 
1591 out:
1592 	mutex_unlock(&cpu_on_off_mutex);
1593 #endif
1594 }
1595 
rockchip_system_monitor_temp_cpu_on_off(int temp)1596 static void rockchip_system_monitor_temp_cpu_on_off(int temp)
1597 {
1598 	bool is_temp_offline;
1599 
1600 	if (cpumask_empty(&system_monitor->temp_offline_cpus))
1601 		return;
1602 
1603 	if (temp > system_monitor->offline_cpus_temp)
1604 		is_temp_offline = true;
1605 	else if (temp < system_monitor->offline_cpus_temp -
1606 		 system_monitor->temp_hysteresis)
1607 		is_temp_offline = false;
1608 	else
1609 		return;
1610 
1611 	if (system_monitor->is_temp_offline == is_temp_offline)
1612 		return;
1613 	system_monitor->is_temp_offline = is_temp_offline;
1614 	rockchip_system_monitor_cpu_on_off();
1615 }
1616 
rockchip_system_monitor_thermal_update(void)1617 static void rockchip_system_monitor_thermal_update(void)
1618 {
1619 	int temp, ret;
1620 	struct monitor_dev_info *info;
1621 
1622 	ret = thermal_zone_get_temp(system_monitor->tz, &temp);
1623 	if (ret || temp == THERMAL_TEMP_INVALID)
1624 		goto out;
1625 
1626 	dev_dbg(system_monitor->dev, "temperature=%d\n", temp);
1627 
1628 	if (temp < system_monitor->last_temp &&
1629 	    system_monitor->last_temp - temp <= 2000)
1630 		goto out;
1631 	system_monitor->last_temp = temp;
1632 
1633 	rockchip_system_monitor_temp_notify(temp);
1634 
1635 	down_read(&mdev_list_sem);
1636 	list_for_each_entry(info, &monitor_dev_list, node)
1637 		rockchip_system_monitor_wide_temp_adjust(info, temp);
1638 	up_read(&mdev_list_sem);
1639 
1640 	rockchip_system_monitor_temp_cpu_on_off(temp);
1641 
1642 out:
1643 	mod_delayed_work(system_freezable_wq, &system_monitor->thermal_work,
1644 			 msecs_to_jiffies(system_monitor->delay));
1645 }
1646 
rockchip_system_monitor_thermal_check(struct work_struct * work)1647 static void rockchip_system_monitor_thermal_check(struct work_struct *work)
1648 {
1649 	if (atomic_read(&monitor_in_suspend))
1650 		return;
1651 
1652 	rockchip_system_monitor_thermal_update();
1653 }
1654 
rockchip_system_status_cpu_limit_freq(struct monitor_dev_info * info,unsigned long status)1655 static void rockchip_system_status_cpu_limit_freq(struct monitor_dev_info *info,
1656 						  unsigned long status)
1657 {
1658 	unsigned int target_freq = 0;
1659 
1660 	if (!freq_qos_request_active(&info->min_sta_freq_req))
1661 		return;
1662 	if (!freq_qos_request_active(&info->max_sta_freq_req))
1663 		return;
1664 
1665 	if (status & SYS_STATUS_REBOOT) {
1666 		freq_qos_update_request(&info->max_sta_freq_req,
1667 					info->reboot_freq);
1668 		freq_qos_update_request(&info->min_sta_freq_req,
1669 					info->reboot_freq);
1670 		return;
1671 	}
1672 
1673 	if (info->video_4k_freq && (status & SYS_STATUS_VIDEO_4K))
1674 		target_freq = info->video_4k_freq;
1675 
1676 	if (target_freq == info->status_max_limit)
1677 		return;
1678 	info->status_max_limit = target_freq;
1679 	if (info->status_max_limit)
1680 		freq_qos_update_request(&info->max_sta_freq_req,
1681 					info->status_max_limit);
1682 	else
1683 		freq_qos_update_request(&info->max_sta_freq_req,
1684 					FREQ_QOS_MAX_DEFAULT_VALUE);
1685 }
1686 
rockchip_system_status_limit_freq(unsigned long status)1687 static void rockchip_system_status_limit_freq(unsigned long status)
1688 {
1689 	struct monitor_dev_info *info;
1690 
1691 	down_read(&mdev_list_sem);
1692 	list_for_each_entry(info, &monitor_dev_list, node) {
1693 		if (info->devp->type == MONITOR_TYPE_CPU)
1694 			rockchip_system_status_cpu_limit_freq(info, status);
1695 	}
1696 	up_read(&mdev_list_sem);
1697 }
1698 
rockchip_system_status_cpu_on_off(unsigned long status)1699 static void rockchip_system_status_cpu_on_off(unsigned long status)
1700 {
1701 	struct cpumask offline_cpus;
1702 
1703 	if (cpumask_empty(&system_monitor->video_4k_offline_cpus))
1704 		return;
1705 
1706 	cpumask_clear(&offline_cpus);
1707 	if (status & SYS_STATUS_VIDEO_4K)
1708 		cpumask_copy(&offline_cpus,
1709 			     &system_monitor->video_4k_offline_cpus);
1710 	if (cpumask_equal(&offline_cpus, &system_monitor->status_offline_cpus))
1711 		return;
1712 	cpumask_copy(&system_monitor->status_offline_cpus, &offline_cpus);
1713 	rockchip_system_monitor_cpu_on_off();
1714 }
1715 
rockchip_system_status_notifier(struct notifier_block * nb,unsigned long status,void * ptr)1716 static int rockchip_system_status_notifier(struct notifier_block *nb,
1717 					   unsigned long status,
1718 					   void *ptr)
1719 {
1720 	rockchip_system_status_limit_freq(status);
1721 
1722 	rockchip_system_status_cpu_on_off(status);
1723 
1724 	return NOTIFY_OK;
1725 }
1726 
rockchip_system_monitor_set_cpu_uevent_suppress(bool is_suppress)1727 static int rockchip_system_monitor_set_cpu_uevent_suppress(bool is_suppress)
1728 {
1729 	struct monitor_dev_info *info;
1730 	struct cpufreq_policy *policy;
1731 
1732 	list_for_each_entry(info, &monitor_dev_list, node) {
1733 		if (info->devp->type != MONITOR_TYPE_CPU)
1734 			continue;
1735 		policy = (struct cpufreq_policy *)info->devp->data;
1736 		if (!policy || !policy->cdev)
1737 			continue;
1738 		if (is_suppress)
1739 			dev_set_uevent_suppress(&policy->cdev->device, 1);
1740 		else
1741 			dev_set_uevent_suppress(&policy->cdev->device, 0);
1742 	}
1743 
1744 	return 0;
1745 }
1746 
monitor_pm_notify(struct notifier_block * nb,unsigned long mode,void * _unused)1747 static int monitor_pm_notify(struct notifier_block *nb,
1748 			     unsigned long mode, void *_unused)
1749 {
1750 	switch (mode) {
1751 	case PM_HIBERNATION_PREPARE:
1752 	case PM_RESTORE_PREPARE:
1753 	case PM_SUSPEND_PREPARE:
1754 		atomic_set(&monitor_in_suspend, 1);
1755 		rockchip_system_monitor_set_cpu_uevent_suppress(true);
1756 		break;
1757 	case PM_POST_HIBERNATION:
1758 	case PM_POST_RESTORE:
1759 	case PM_POST_SUSPEND:
1760 		if (system_monitor->tz)
1761 			rockchip_system_monitor_thermal_update();
1762 		atomic_set(&monitor_in_suspend, 0);
1763 		rockchip_system_monitor_set_cpu_uevent_suppress(false);
1764 		system_monitor->last_temp = INT_MAX;
1765 		break;
1766 	default:
1767 		break;
1768 	}
1769 	return 0;
1770 }
1771 
1772 static struct notifier_block monitor_pm_nb = {
1773 	.notifier_call = monitor_pm_notify,
1774 };
1775 
rockchip_monitor_reboot_notifier(struct notifier_block * nb,unsigned long action,void * ptr)1776 static int rockchip_monitor_reboot_notifier(struct notifier_block *nb,
1777 					     unsigned long action, void *ptr)
1778 {
1779 	rockchip_set_system_status(SYS_STATUS_REBOOT);
1780 	if (system_monitor->tz)
1781 		cancel_delayed_work_sync(&system_monitor->thermal_work);
1782 
1783 	return NOTIFY_OK;
1784 }
1785 
1786 static struct notifier_block rockchip_monitor_reboot_nb = {
1787 	.notifier_call = rockchip_monitor_reboot_notifier,
1788 };
1789 
rockchip_monitor_fb_notifier(struct notifier_block * nb,unsigned long action,void * ptr)1790 static int rockchip_monitor_fb_notifier(struct notifier_block *nb,
1791 					unsigned long action, void *ptr)
1792 {
1793 	struct fb_event *event = ptr;
1794 
1795 	if (action != FB_EVENT_BLANK)
1796 		return NOTIFY_OK;
1797 
1798 	switch (*((int *)event->data)) {
1799 	case FB_BLANK_UNBLANK:
1800 		rockchip_clear_system_status(SYS_STATUS_SUSPEND);
1801 		break;
1802 	case FB_BLANK_POWERDOWN:
1803 		rockchip_set_system_status(SYS_STATUS_SUSPEND);
1804 		break;
1805 	default:
1806 		break;
1807 	}
1808 
1809 	return NOTIFY_OK;
1810 }
1811 
1812 static struct notifier_block rockchip_monitor_fb_nb = {
1813 	.notifier_call = rockchip_monitor_fb_notifier,
1814 };
1815 
rockchip_eink_devfs_notifier(struct notifier_block * nb,unsigned long action,void * ptr)1816 static int rockchip_eink_devfs_notifier(struct notifier_block *nb,
1817 					unsigned long action, void *ptr)
1818 {
1819 	switch (action) {
1820 	case EBC_ON:
1821 		rockchip_clear_system_status(SYS_STATUS_LOW_POWER);
1822 		break;
1823 	case EBC_OFF:
1824 		rockchip_set_system_status(SYS_STATUS_LOW_POWER);
1825 		break;
1826 	default:
1827 		break;
1828 	}
1829 
1830 	return NOTIFY_OK;
1831 }
1832 
1833 static struct notifier_block rockchip_monitor_ebc_nb = {
1834 	.notifier_call = rockchip_eink_devfs_notifier,
1835 };
1836 
system_monitor_early_min_volt_function(struct work_struct * work)1837 static void system_monitor_early_min_volt_function(struct work_struct *work)
1838 {
1839 	struct monitor_dev_info *info;
1840 	struct regulator_dev *rdev;
1841 	int min_uV, max_uV;
1842 	int ret;
1843 
1844 	down_read(&mdev_list_sem);
1845 	list_for_each_entry(info, &monitor_dev_list, node) {
1846 		if (!info->early_min_volt || !info->early_reg)
1847 			continue;
1848 		rdev = info->early_reg->rdev;
1849 		min_uV = rdev->constraints->min_uV;
1850 		max_uV = rdev->constraints->max_uV;
1851 		ret = regulator_set_voltage(info->early_reg, min_uV, max_uV);
1852 		if (ret)
1853 			dev_err(&rdev->dev,
1854 				"%s: failed to set volt\n", __func__);
1855 		regulator_put(info->early_reg);
1856 	}
1857 	up_read(&mdev_list_sem);
1858 }
1859 
1860 static DECLARE_DELAYED_WORK(system_monitor_early_min_volt_work,
1861 			    system_monitor_early_min_volt_function);
1862 
rockchip_system_monitor_probe(struct platform_device * pdev)1863 static int rockchip_system_monitor_probe(struct platform_device *pdev)
1864 {
1865 	struct device *dev = &pdev->dev;
1866 
1867 	system_monitor = devm_kzalloc(dev, sizeof(struct system_monitor),
1868 				      GFP_KERNEL);
1869 	if (!system_monitor)
1870 		return -ENOMEM;
1871 	system_monitor->dev = dev;
1872 
1873 	system_monitor->kobj = kobject_create_and_add("system_monitor", NULL);
1874 	if (!system_monitor->kobj)
1875 		return -ENOMEM;
1876 	if (sysfs_create_file(system_monitor->kobj, &status.attr))
1877 		dev_err(dev, "failed to create system status sysfs\n");
1878 
1879 	cpumask_clear(&system_monitor->status_offline_cpus);
1880 	cpumask_clear(&system_monitor->offline_cpus);
1881 
1882 	rockchip_system_monitor_parse_dt(system_monitor);
1883 	if (system_monitor->tz) {
1884 		system_monitor->last_temp = INT_MAX;
1885 		INIT_DELAYED_WORK(&system_monitor->thermal_work,
1886 				  rockchip_system_monitor_thermal_check);
1887 		mod_delayed_work(system_freezable_wq,
1888 				 &system_monitor->thermal_work,
1889 				 msecs_to_jiffies(system_monitor->delay));
1890 	}
1891 
1892 	system_monitor->status_nb.notifier_call =
1893 		rockchip_system_status_notifier;
1894 	rockchip_register_system_status_notifier(&system_monitor->status_nb);
1895 
1896 	if (register_pm_notifier(&monitor_pm_nb))
1897 		dev_err(dev, "failed to register suspend notifier\n");
1898 
1899 	register_reboot_notifier(&rockchip_monitor_reboot_nb);
1900 
1901 	if (fb_register_client(&rockchip_monitor_fb_nb))
1902 		dev_err(dev, "failed to register fb nb\n");
1903 
1904 	ebc_register_notifier(&rockchip_monitor_ebc_nb);
1905 
1906 	schedule_delayed_work(&system_monitor_early_min_volt_work,
1907 			      msecs_to_jiffies(30000));
1908 
1909 	dev_info(dev, "system monitor probe\n");
1910 
1911 	return 0;
1912 }
1913 
1914 static const struct of_device_id rockchip_system_monitor_of_match[] = {
1915 	{
1916 		.compatible = "rockchip,system-monitor",
1917 	},
1918 	{ /* sentinel */ },
1919 };
1920 MODULE_DEVICE_TABLE(of, rockchip_system_monitor_of_match);
1921 
1922 static struct platform_driver rockchip_system_monitor_driver = {
1923 	.probe	= rockchip_system_monitor_probe,
1924 	.driver = {
1925 		.name	= "rockchip-system-monitor",
1926 		.of_match_table = rockchip_system_monitor_of_match,
1927 	},
1928 };
1929 module_platform_driver(rockchip_system_monitor_driver);
1930 
1931 MODULE_LICENSE("GPL v2");
1932 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
1933 MODULE_DESCRIPTION("rockchip system monitor driver");
1934